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.


In reply to Re: sort based on date by ikegami
in thread sort based on date by suhailck

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.