Hi Robin,

You can more then double your performance with DateTime::Format::Strptime by
instantiating a parser object and reusing it.  You should also keep in mind
that Time::Piece doesn't really support leapseconds (it may be able to parse
them on some platforms if it's using gmtime(3)...  haven't looked at the
source).

--
use Time::Piece;
use Benchmark qw( cmpthese );
use DateTime::Format::Strptime qw( strptime );

my $dtstrp = DateTime::Format::Strptime->new( pattern => "%Y-%m-%dT%H%M%S");

cmpthese ( -3,
    {
        'DT Strptime' => sub {
            $dtstrp->parse_datetime("2004-10-10T101010");
        },
        'DT Strptime imported' => sub {
            strptime("%Y-%m-%dT%H%M%S", "2004-10-10T101010");
        },
        'Time::Piece' => sub {
            Time::Piece->strptime("2004-10-10T101010", "%Y-%m-%dT%H%M%S");
        },
    }

);
--

                        Rate DT Strptime imported     DT Strptime    Time::Piece
DT Strptime imported  1338/s                   --            -54%           -98%
DT Strptime           2916/s                 118%              --           -97%
Time::Piece          85687/s                6305%           2839%             --

Cheers,

-J

--

On Thu, 9 Sep 2004, Robin Phillips wrote:

> Hi,
> 
> I have been converting a couple of programs that were using Time::Piece as 
> their date/time handling module, to use DateTime.pm instead. 
> 
> However it seems that DateTime.pm apears to be about 700 times slower 
> than Time::Piece when parsing dates.
> 
> The little program below takes 15 seconds to do 10,000 calls to the new 
> strptime and only 2 seconds to do 100,000 (ie 10x more) calls to the old 
> version (on my machine).
> 
> Is this something to do with Time::Piece using a C library and DateTime 
> doing things in perl instead? Or is there some otherway of doing this date 
> parsing using DateTime that is faster?
> 
> Robin
> 
> -------------------------------------------------
> #!/usr/bin/perl -w
> use strict;
> use DateTime ;
> use DateTime::Format::Strptime qw{strptime};
> use Time::Piece;
> 
> print gmtime->datetime, "\n";
> for (my $i =0; $i <=10_000; $i++) {
>       my $file_datetime = strptime("%Y-%m-%dT%H%M%S", "2004-10-10T101010");
> }
> 
> print gmtime->datetime, "\n";
> 
> for (my $i =0; $i <=100_000; $i++) {
>       my $file_datetime = Time::Piece -> strptime("2004-10-10T101010", 
> "%Y-%m-%dT%H%M%S");
> }
> 
> print gmtime->datetime, "\n";
> 
> exit;
> 
> 
> 
> 
> 

Reply via email to