I suspect this is a case of premature optimisation, but assuming this really is a bottleneck, the biggest loss of time would be calling the Perl compare function repeatedly. Using a basic lexical or numerical sort would be better. (The default callback, { $a cmp $b } and { $a <=> $b } are handled super efficiently.)
use 5.010; # say
use strict;
use warnings;
use Date::Manip qw( ParseDateString );
my @dates = (
"Aug 8 12:12:13",
"Jan 1 21:01:03",
"Aug 8 12:12:14",
"Aug 7 09:12:10",
);
my %lookup;
say $lookup{$_}
for
sort
map { $lookup{ my $s = ParseDateString($_) } = $_; $s }
@dates;
And that can be optimised to
use 5.010; # say
use strict;
use warnings;
use Date::Manip qw( ParseDateString );
my @dates = (
"Aug 8 12:12:13",
"Jan 1 21:01:03",
"Aug 8 12:12:14",
"Aug 7 09:12:10",
);
say substr($_, 16)
for
sort
map ParseDateString($_).$_,
@dates;
The following might be faster by taking advantage of internal optimisations.
use 5.010; # say
use strict;
use warnings;
use Date::Manip qw( ParseDateString );
my @dates = (
"Aug 8 12:12:13",
"Jan 1 21:01:03",
"Aug 8 12:12:14",
"Aug 7 09:12:10",
);
$_ = ParseDateString($_).$_ for @dates;
@dates = sort @dates;
say substr($_, 16) for @dates;
I also agree with the previous comment about replacing ParseDateString with something tailored to your needs. Mind you, if you write the replacement in Perl and if ParseDateString is currently written in C, the replacement could easily be slower.
I haven't benchmarked anything.
|