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


in reply to List of directory (3 ways)

I think your results are so surprising because your benchmark code adds a lot of unneccessary cruft to the different approaches. For example, in method 1 you're instantiating my $file at every pass through the loop.

FWIW, here's my benchmark of approach 1 (iterative) vs. approach 3 (slurpy):

use Benchmark qw/:all/; my $folder = "C:\\Perl"; sub iterative { opendir $DIR, '.'; my $file; while ($file = readdir $DIR) { $file = "$folder\\$file"; next unless -f $file; my @s = stat $file; } closedir $DIR; # I like being explicit; } sub slurpy { opendir $DIR, '.'; my @files = map { "$folder\\$_" } grep { -f $_ } readdir($DIR); foreach (@files) { my @s = stat $_; } closedir $DIR; } cmpthese ( 200, { 'iterative' => \&iterative, 'slurpy' => \&slurpy, }); __END__ Rate slurpy iterative slurpy 74.4/s -- -44% iterative 133/s 79% --

As you can see, the iterative approach is *much* faster in my benchmark. (The other variable is that I'm running WinXP). I have similar results on OSX (FreeBSD-based "Darwin"), see below.

C:\>perl -v This is perl, v5.8.6 built for MSWin32-x86-multi-thread (with 3 registered patches, see perl -V for more detail)

My test on my home OSX machine:

Rate slurpy iterative slurpy 1264/s -- -17% iterative 1517/s 20% --
$ perl -v This is perl, v5.8.6 built for darwin-thread-multi-2level (with 2 registered patches, see perl -V for more detail)

Updates:

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet