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 971240.pl 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 971240.pl #!/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 (Patriarch) on May 19, 2012 at 21:33 UTC |