Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi,
at some other forum we had the discussion which is the fastest way to read all files of a directory
  • opendir $DIR, $folder; while(my $file = readdir $DIR) ...
  • foreach my $file (glob "$folder/*") ...
  • opendir $DIR, $folder; my @files = grep { ... } readdir $DIR; foreach my $file (@files) ...
i prefered 1 over 3 because i thought 1 must be faster since 3 will walk all files (at least) twice. so i made a benchmark:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); my %folders = ( Temp1 => 10, Temp2 => 100, Temp3 => 1000, Temp4 => 5000, Temp5 => 10000, # Temp6 => 20000, ); foreach my $folder (sort keys %folders) { print "Folder: $folder, Files: ", $folders{$folder}, "\n"; timethese(5, { 'Way 1' => sub { opendir(my $DIR, $folder) or die "Error: couldn't open dir + '$folder': $!\n"; while(my $file = readdir $DIR) { my $filename = "$folder/$file"; if(-f $filename) { # print STDERR "$filename\n"; } } closedir($DIR); }, 'Way 2' => sub { foreach my $file (glob "$folder/*") { my $filename = $file; if(-f $filename) { # print STDERR "$filename\n"; } } }, 'Way 3' => sub { opendir(my $DIR, $folder) or die "Error: couldn't open dir + '$folder': $!\n"; my @files = map { "$folder/$_" } grep { -f "$folder/$_" } readdir($DIR); closedir($DIR); foreach my $file (@files) { my $filename = $file; # print STDERR "$filename\n"; } }, }); }
the result does not make sence to me, but maybe you can tell me why
Folder: Temp1, Files: 10
Benchmark: timing 5 iterations of Way 1, Way 2, Way 3...
   Way 1:  0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
           (warning: too few iterations for a reliable count)
   Way 2:  0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
           (warning: too few iterations for a reliable count)
   Way 3:  0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
           (warning: too few iterations for a reliable count)
Folder: Temp2, Files: 100
Benchmark: timing 5 iterations of Way 1, Way 2, Way 3...
   Way 1:  0 wallclock secs ( 0.00 usr +  0.05 sys =  0.05 CPU) @ 108.70/s (n=5)
           (warning: too few iterations for a reliable count)
   Way 2:  0 wallclock secs ( 0.00 usr +  0.08 sys =  0.08 CPU) @ 63.29/s (n=5)
           (warning: too few iterations for a reliable count)
   Way 3:  0 wallclock secs ( 0.03 usr +  0.00 sys =  0.03 CPU) @ 161.29/s (n=5)
           (warning: too few iterations for a reliable count)
Folder: Temp3, Files: 1000
Benchmark: timing 5 iterations of Way 1, Way 2, Way 3...
   Way 1:  1 wallclock secs ( 0.19 usr +  0.63 sys =  0.81 CPU) @  6.15/s (n=5)
   Way 2:  1 wallclock secs ( 0.41 usr +  1.11 sys =  1.52 CPU) @  3.30/s (n=5)
   Way 3:  1 wallclock secs ( 0.16 usr +  0.67 sys =  0.83 CPU) @  6.04/s (n=5)
Folder: Temp4, Files: 5000
Benchmark: timing 5 iterations of Way 1, Way 2, Way 3...
   Way 1:  9 wallclock secs ( 1.20 usr +  7.86 sys =  9.06 CPU) @  0.55/s (n=5)
   Way 2: 19 wallclock secs ( 1.73 usr + 16.59 sys = 18.33 CPU) @  0.27/s (n=5)
   Way 3: 10 wallclock secs ( 0.92 usr +  7.91 sys =  8.83 CPU) @  0.57/s (n=5)
Folder: Temp5, Files: 10000
Benchmark: timing 5 iterations of Way 1, Way 2, Way 3...
   Way 1: 43 wallclock secs ( 3.17 usr + 39.39 sys = 42.56 CPU) @  0.12/s (n=5)
   Way 2: 85 wallclock secs ( 6.53 usr + 76.41 sys = 82.94 CPU) @  0.06/s (n=5)
   Way 3: 42 wallclock secs ( 3.19 usr + 38.06 sys = 41.25 CPU) @  0.12/s (n=5)

In reply to 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":



  • 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-02-06 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (33 votes). Check out past polls.

    Notices?