Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Re: Things every perl programmer should know? (faster?)

by runrig (Abbot)
on Jun 04, 2003 at 23:15 UTC ( #263173=note: print w/replies, xml ) Need Help??

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

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?

Replies are listed 'Best First'.
Re: Re: Re: Things every perl programmer should know? (faster?)
by BrowserUk (Pope) on Jun 04, 2003 at 23:32 UTC

    You don't mention which version you ran your test on, but if it was 5.8, part of the reason could be that glob now returns the files pre-sorted (according to perldelta).

    However, I think that tye was alluding to that the OP's code shows not just reading list of files, but looping over them, opening and reading each one in turn. Assuming that the opening and reading part stayed the same, 2 or 3 seconds on generating the list is unlikely to make a significant difference overall.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re^3: Things every perl programmer should know? (faster?)
by tye (Sage) on Jun 05, 2003 at 14:15 UTC

    Thanks. It is nice to have some numbers and some corroboration. 3 seconds is almost shockingly slow to just read 10000 filenames and compare to a glob string. Especially since I thought Perl is using C code for this (taken from some well-respected free shell implementation, I think).

    I also appreciate the "I'd do it with glob [... unless] embedded in a library", which matches my thinking on the subject (except that I think glob can probably be "fixed" and so I might even use glob in a library based on that expectation).

    So I'm still curious why glob is so slow. I'm not sure when I'll find the time/motivation to investigate further (probably comparing built-in glob vs. File::Glob, File::DosGlob, and my own File::KGlob and verifying some of my own assumptions).

    I'm also still interested in seeing numbers from the original poster. It sounds like you would see less than 2 seconds difference in run time in a similar situation. I have a hard time characterizing 2 seconds as an "impressive" difference, but I've seen such characterizations made based on the ratio of run times so that might be the whole story. But it could also be that licking9Volts was seeing a much bigger difference.

                    - tye

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://263173]
[davido]: Lol, in composing test code for Re: Stupid, yet simple, sort question I managed to fill up /tmp for a moment, causing bash to complain about "pmbash: cannot create temp file for here-document: No space left on device"
[davido]: hm, sorry, wrong link: Re^3: Perl Program to efficiently process 500000 small files in a Directory (AIX) should be it.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2018-03-18 06:59 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (228 votes). Check out past polls.