Update: Just before someone reads this, you might want
to no that this does not answer the original question.
I've run the test, and got these results:
Benchmark: running Ordinary, Schwartzian for at least 2 CPU seconds...
Ordinary: 2 wallclock secs ( 1.02 usr + 1.15 sys = 2.17 CPU) @ 55
+.30/s (n=120)
Schwartzian: 2 wallclock secs ( 1.81 usr + 0.36 sys = 2.17 CPU) @ 8
+8.02/s (n=191)
So the Schwartzian transform is faster for me.
I have 170 files in the /bin directory.
Update:
I've moved the globbing out as merlyn has suggested.
I've also added a new sorting variant.
The code is:
#!perl
use warnings;
use strict;
use Benchmark qw(timethese);
our(@all, @sorted, @results);
for my $dir ("/bin/", "/usr/bin/") {
my $D;
opendir $D, $dir;
@all = readdir $D;
closedir $D;
chdir $dir;
print "$dir contains ".@all." files\n";
sub sort_ord {
@sorted = sort { -s $a <=> -s $b } @all;
}
sub sort_sch {
@results =
map $_->[0],
sort { $a->[1] <=> $b->[1] }
map [$_, -s $_],
@all;
}
sub sort_new {
my %h;
@h{@all} = map {-s $_} @all;
@results = sort { $h{$a}<=>$h{$b} } @all;
}
sub cmp_them {
join("\n", @sorted) eq join("\n", @results) or
die "bad sort";
}
sort_ord;
sort_sch; cmp_them;
sort_new; cmp_them;
timethese -5, {
Ordinary => \&sort_ord,
Schwartzian => \&sort_sch,
Strange => \&sort_new,
};
}
My results are:
/bin/ contains 172 files
Benchmark: running Ordinary, Schwartzian, Strange for at least 5 CPU s
+econds...
Ordinary: 6 wallclock secs ( 2.18 usr + 3.08 sys = 5.26 CPU) @ 79
+.09/s (n=416)
Schwartzian: 5 wallclock secs ( 4.69 usr + 0.58 sys = 5.27 CPU) @ 1
+34.72/s (n=710)
Strange: 5 wallclock secs ( 4.60 usr + 0.64 sys = 5.24 CPU) @ 17
+8.44/s (n=935)
/usr/bin/ contains 1397 files
Benchmark: running Ordinary, Schwartzian, Strange for at least 5 CPU s
+econds...
Ordinary: 5 wallclock secs ( 1.91 usr + 3.15 sys = 5.06 CPU) @ 6
+.32/s (n=32)
Schwartzian: 5 wallclock secs ( 4.73 usr + 0.52 sys = 5.25 CPU) @ 1
+2.95/s (n=68)
Strange: 5 wallclock secs ( 4.67 usr + 0.55 sys = 5.22 CPU) @ 15
+.33/s (n=80)
I am surprised that Strange is a bit faster than Schwartzian, I thought it must be a little slower.
Btw, the two extra file in /bin are just . and .. that glob("/bin/*")
do not return.
-
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.