Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
What I, as a Perl programmer, would like to know, is some estimate of how much slower glob is than readdir. How about some numbers based on what you saw as one source of data? You said over 4000 files and show the contents of the files being read. How long did the different versions take? About how large were the files you were reading?
I was curious myself, so I created a directory with 10000 files (zero size), half of them with the extension ".tmp". I wrote equivalent readdir and glob statements to get all of the *.tmp files, and the glob is noticeably slower. It takes about 3 seconds while the readdir with grep seems instantaneous. This is just a rough eyeball benchmark, but still, the glob is definitely slower. I even switched the order of the directory reads, and got the same result. Here is the code (this was run on an old AIX system):
$|++; print "Glob\n"; my @file2 = glob("tmp/*.tmp"); my $count2 = @file2; print "$count2\n"; print "Readdir\n"; opendir(DIR, "tmp") or die "Acck: $!"; my @file1 = grep /\.tmp$/, readdir DIR; my $count1 = @file1; print "$count1\n"; closedir DIR; print "Done\n";
I'd say if the 3 seconds doesn't matter, I'd do it with glob to save myself the coding. If this were embedded in a library that I expected other people to use, I'd do it with readdir 'just in case' speed matters.

Update:This was perl 5.6.1 (and I received the same results with File::Glob::bsd_glob and the GLOB_NOSORT option).

Another update: I vaguely remember glob being painfully slow on Windows at one time, which has since been fixed (I think). And it may have been only with the angle bracket syntax...can anyone confirm?


In reply to Re: Re: Things every perl programmer should know? (faster?) by runrig
in thread Things every perl programmer should know? by licking9Volts

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?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found