Date-Reformat

 view release on metacpan or  search on metacpan

examples/benchmark_date_reformat.pl  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;

use Benchmark;
use Date::Reformat;

my $strptime = '%Y-%m-%d %H:%M:%S%Z';
my $date_string = '2015-01-14 21:07:31Z';

my $strptime_parser = Date::Reformat->new(
    'parser' => {
        'strptime' => $strptime,
    },
    'formatter' => {
        'strftime' => $strptime,
    },
);

my $heuristic_parser = Date::Reformat->new(
    'parser' => {
        'heuristic' => 'ymd',
    },
    'formatter' => {
        'strftime' => $strptime,
    },
);

my $tests = [
    {
        'label'      => 'Initialize',
        'iterations' => 10000,
        'coderef'    => sub {
            my $test_parser = Date::Reformat->new(
                'parser' => {
                    'strptime' => $strptime,
                },
            );
        },
    },
    {
        'label'      => 'Parse strptime',
        'iterations' => 100000,
        'coderef'    => sub {
            my $test_output = $strptime_parser->parse_date($date_string);
        },
    },
    {
        'label'      => 'Parse heuristic',
        'iterations' => 100000,
        'coderef'    => sub {
            my $test_output = $heuristic_parser->parse_date($date_string);
        },
    },
    {
        'label'      => 'Reformat strptime',
        'iterations' => 100000,
        'coderef'    => sub {
            my $test_output = $strptime_parser->reformat_date($date_string);
        },
    },
    {
        'label'      => 'Reformat heuristic',
        'iterations' => 100000,
        'coderef'    => sub {
            my $test_output = $heuristic_parser->reformat_date($date_string);
        },
    },
];

foreach my $test (@$tests) {
    Benchmark::timethis(
        $test->{'iterations'},
        $test->{'coderef'},
        $test->{'label'},
    );
}



( run in 0.502 second using v1.01-cache-2.11-cpan-71847e10f99 )