http://qs1969.pair.com?node_id=384956


in reply to Benchmark, -s versus schwartzian

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.