2014-01-30 Rick Measham <r...@measham.id.au>:

>
> Second, and most importantly, your benchmark should fare MUCH better if
> you don't create a new parser every time. Create the parser outside the
> benchmark sub and it should be muuuuuuch quicker. And that is how your
> production code should work.
>
>
The code takes care to create the parser only once in a 'state' variable.
But this state variable will be initialized on the first run, not at
compile time. So this will still impact the performance of the first run.
But that first run is not the one that is run in cmptheese because there is
a first run at line 112. So I don't see an issue in that benchmark.


Anyway I would still suggest to write the code differently to always avoid
the 'state initialization issue' in benchmarks: use a closure instead of a
'state' variable.

Original code:
  'strptime' => sub {
        state $parser = DateTime::Format::Strptime->new(
            pattern => '%F %T.%N',
            locale => 'C',
            time_zone => 'Europe/Warsaw',
            on_error => 'croak',
           );
        return $parser->parse_datetime(@_);
    },

Modified code:
  'strptime' => do {
        my $parser = DateTime::Format::Strptime->new(
            pattern => '%F %T.%N',
            locale => 'C',
            time_zone => 'Europe/Warsaw',
            on_error => 'croak',
           );
        sub { $parser->parse_datetime(@_) }
    },

Olivier.

Reply via email to