Hi,
at some other forum we had the discussion which is the fastest way to read all files of a directory
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":



  • 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.