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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.