[Memphis.pm] sorts
Carl Brock Sides
csides at autozone.com
Fri Oct 6 10:58:00 CDT 2000
* Keith W. Sheffield <sheff at pobox.com> [001005 23:22]:
> I though of ways to compact the sort demos given tonight and I got the
> following scripts at the end of this post.
>
> (listing 1 is faster, BTW).
Strange, listing 2 is faster on my machine.
csides at tanuki:~/sorting$ time ./keith1.pl < ips > /dev/null
real 0m5.123s
user 0m4.990s
sys 0m0.110s
csides at tanuki:~/sorting$ time ./keith2.pl < ips > /dev/null
real 0m2.982s
user 0m2.840s
sys 0m0.120s
Using Benchmark.pm shows that the single pack is a lot more efficient
than the join/map/sprintf combo:
csides at tanuki:~/sorting$ cat benchmark.pl
#!/usr/local/perl-5.6/bin/perl -w
use Benchmark;
timethese(100000, {
keith1 => 'join("", map {sprintf "%03d", $_;} split(/\./,
"192.168.0.1"))',
keith2 => 'pack("C4", split( /\./, "192.168.0.1"))'
});
__END__
csides at tanuki:~/sorting$ ./benchmark.pl
Benchmark: timing 100000 iterations of keith1, keith2...
Useless use of join in void context at (eval 2) line 1.
keith1: 20 wallclock secs (20.84 usr + 0.00 sys = 20.84 CPU) @
4798.46/s (n=100000)
Useless use of pack in void context at (eval 4) line 1.
keith2: 6 wallclock secs ( 6.88 usr + 0.03 sys = 6.91 CPU) @
14471.78/s (n=100000)
> ---- listing 1 ----
> #!/usr/bin/perl
> {
> local $,="\n";
> print map {$_->[0]} sort {$a->[1] <=> $b->[1]} map {chomp; [ $_,
> join('', map { sprintf "%03d", $_;} split(/\./)) ]; } <>;
> print "\n";
> }
> -----
> ----- listing 2 ---
> #!/usr/bin/perl
> {
> local $,="\n";
> print map {$_->[0]} sort {$a->[1] cmp $b->[1]} map {chomp; [ $_,
> pack("C4", split(/\./)) ]; } <>;
> print "\n";
> }
--
Brock Sides
csides at autozone.com
The original plan [for GNOME] was to aim to make a desktop as good as the Macintosh, and we should not lower our ambition by making one merely as good as Windows. -- RMS
----------------------------------------------------------------------------
To unsubscribe, please send email to majordomo at pm.org
with 'unsubscribe memphis-pm-list' in the body of the message.
----------------------------------------------------------------------------
More information about the Memphis-pm
mailing list