Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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)


  • 2005-01.Jan-17 : OSX info added

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

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

  • 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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-02-06 06:36 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.