Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

by aaron_baugher (Curate)
on May 19, 2012 at 19:32 UTC ( #971437=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re^3: Date to be sorted in descending and time in ascending
by BrowserUk (Pope) on May 19, 2012 at 21:33 UTC

    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://971437]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-04-25 11:49 GMT
Find Nodes?
    Voting Booth?