Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by tye (Cardinal)
on Jun 04, 2003 at 21:52 UTC ( #263161=note: print w/ replies, xml ) Need Help??


in reply to Things every perl programmer should know?

I'd like to know why globbing is so much slower than readdir. Sure, that makes sense for really old versions of Perl (where the slowness of glob is insignificant to the security problems of it).

I could even see glob being slightly slower than readdir, such that you likely wouldn't notice the difference, especially in a script (like your example) that does something other than list file names.

But you say using glob makes your whole script a lot slower ("impressive"). 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 often prefer glob as it often makes for less code (and has fewer 'gothca's). But if it is hugely slower, then I'll prefer it less often.

Thanks.

                - tye


Comment on Re: Things every perl programmer should know? (faster?)
Re: Re: Things every perl programmer should know? (faster?)
by runrig (Abbot) on Jun 04, 2003 at 23:15 UTC
    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?

      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


      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
Re: Re: Things every perl programmer should know? (faster?)
by licking9Volts (Pilgrim) on Jun 05, 2003 at 18:07 UTC
    The files I work with are actually logs that record information at set intervals down an oil well's borehole. So, depending on the depths of these logs, I have files that range in size from around 100kb to 50mb. My program only needs to read from a block of information at the top, and extract certain bits to a text file. Once it hits a specific delimiter, it closes the file and moves on to the next file. The file name is a 10-14 digit identification number plus an optional version number. When I was globbing the files, it was taking around 5 minutes to get started. With the readdir, it starts immediately.

    I am running perl version 5.6.0 on Win2k. Our machines here at work are managed by off-site personnel and have the registries and "Program Files" folders locked down so I can't upgrade the perl version or make any modifications to it. Any software has to be tested and packaged for company-wide distribution via intranet. It really sucks. So anyway, that's my situation. Here's the node where the readdir was suggested: File glob question. Let me know if you have any suggestions. Thanks!

    licking9Volts

      So readdir isn't necessarily significantly faster, as far as finishing time is concerned---the important factor is the impression of responsiveness, seeing the first results after a short delay instead of after all the file names have been globbed.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://263161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-12-22 04:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls