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

In reply to Re: List of directory (3 ways) by radiantmatrix
in thread List of directory (3 ways) by esskar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.