Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Date to be sorted in descending and time in ascending

by Cristoforo (Deacon)
on May 18, 2012 at 23:27 UTC ( #971373=note: print w/replies, xml ) Need Help??

in reply to Date to be sorted in descending and time in ascending

Using unpack in a Schwartzian Transform.

#!/usr/bin/perl use strict; use warnings; use 5.014; print map {$_->[0]} sort {$b->[1] cmp $a->[1] || $a->[2] cmp $b->[2]} map {[ $_, unpack "a8a6", $_ ]} <DATA>; __DATA__ 20010405000000 20050405005000 20020405081200 20080405022500 20080405022600 20080405023500 20090405022500 20090405022300 20090405022900 20090405022100

Replies are listed 'Best First'.
Re^2: Date to be sorted in descending and time in ascending
by aaron_baugher (Curate) on May 19, 2012 at 19:32 UTC

    I wondered if someone would suggest unpack. I don't use it often enough to remember the format codes without checking the man page, but it seems like it's often the fastest solution for this kind of thing. So I added yours to my benchmark, and found that unpack was slightly slower than substr/substr in this case. (I'd guess that if it were necessary to break the string into three or more pieces, unpack would come out ahead.) Both were still slower than the non-Schwartzian substr/substr sort, as detailed in my other post, though. Results and code:

    bannor:~/work/perl/monks$ perl 1000000 s/iter stunpack stsubst plainsort stunpack 12.4 -- -10% -31% stsubst 11.2 11% -- -24% plainsort 8.54 46% 31% -- bannor:~/work/perl/monks$ cat #!/usr/bin/env perl use Modern::Perl; use Benchmark qw(:all); my @data; push @data, int(rand(1000000000000))+10000000000000 for (1..$ARGV[0]); cmpthese( 10, { 'stunpack' => \&stunpack, 'stsubst' => \&stsubst, 'plainsort' => \&plainsort, }); sub plainsort { my @d = sort { substr($b,0,8) <=> substr($a,0,8) or substr($a,8) <=> substr($b,8) } @data; } sub stsubst { my @d = map { $_->[0] } sort { $b->[1] <=> $a->[1] or $a->[2] <=> $b->[2] } map { [ $_, substr( $_, 0, 8 ), substr( $_, 8)] } @data; } sub stunpack { my @d = map { $_->[0] } sort { $b->[1] <=> $a->[1] or $a->[2] <=> $b->[2] } map { [ $_, unpack "a8a6" ] } @data; }

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      You should add salva's offering to your benchmark.

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://971373]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2017-02-25 21:03 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (368 votes). Check out past polls.