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

Re: Re: Efficient processing of large directory

by BrowserUk (Pope)
on Oct 03, 2003 at 00:10 UTC ( #296130=note: print w/ replies, xml ) Need Help??


in reply to Re: Efficient processing of large directory
in thread Efficient processing of large directory

It's worth noting that if your trying to find a subset of the files contained in a subdir, rather than processing them all, then using <*.txt> is considerably faster that using either File::Find or opendir/readdir/closedir. At least that is the case under Win32 as the wildcard matching is done by the OS and only those files matching are past back.

In the examples below, the first comparison shows selecting all 17576 files in a subdirectory. In this case, glob and File::Find come out pretty much even.

In the second comparison, a subset of 676 files is selected from the 17000 using a wildcard. In this case, the glob runs 650% faster as it is only processing the 676, rather than looping over the whole 17000+.

Of course, if any real processing was being done rather than just counting the files, the difference would rapidly disappear.

In this case, the OP's use of the word "efficient" was most likely to do with the memory used by slurping all 17000 names in to memory rather than speed, but if not all those 17000 file are .txt files, the time saved might be worth having.

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use File::Find; our( $dir, $glob, $re_glob ); my %tests = ( GLOB => q[ my $count; $count++ while <${dir}/${glob}>; + print 'GLOB: ', $count; ], FIND => q[ my $count; find( sub{ m[$re_glob] and $count++ }, $dir +); print 'FIND: ', $count; ], ); ( $dir, $glob, $re_glob ) = ( 'bigdir', '/*.txt', qr[\.txt$] ); cmpthese( 3, \%tests ); ( $dir, $glob, $re_glob ) = ( 'bigdir', '*A.txt', qr[A\.txt$] ); cmpthese( 10, \%tests ); __END__ P:\test>glob-ff FIND: 17576 FIND: 17576 FIND: 17576 GLOB: 17576 GLOB: 17576 GLOB: 17576 s/iter FIND GLOB FIND 29.6 -- -1% GLOB 29.4 1% -- FIND: 676 FIND: 676 FIND: 676 FIND: 676 FIND: 676 FIND: 676 FIND: 676 FIND: 676 FIND: 676 FIND: 676 GLOB: 676 GLOB: 676 GLOB: 676 GLOB: 676 GLOB: 676 GLOB: 676 GLOB: 676 GLOB: 676 GLOB: 676 GLOB: 676 s/iter FIND GLOB FIND 29.6 -- -87% GLOB 3.97 646% --

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
If I understand your problem, I can solve it! Of course, the same can be said for you.


Comment on Re: Re: Efficient processing of large directory
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (18)
As of 2014-10-24 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (132 votes), past polls