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

Re: Optimizing performance for script to traverse on filesystem

by Marshall (Abbot)
on Feb 01, 2012 at 23:45 UTC ( #951317=note: print w/replies, xml ) Need Help??

in reply to Optimizing performance for script to traverse on filesystem

I would definitely recommend using File::Find or one of its variants (and there are some fancy ones) to do the directory scanning. This eliminates the need for you to write any recursive code yourself.

I didn't test the code below and there is bound to be some kind of mistake in it. But this is to give you an idea of another approach.

The most simple variant of File::Find calls a subroutine for every file or directory underneath the starting place. A localized variable $File::Find::name contains the full path of where we currently are (dir name or file name). I suggest that you just run the wanted sub with just the print line at the end (in comments) to see the default order of the descent.

I think this collects the data that you wanted? But not 100% sure that I got everything.

Since you are interested in performance one not so obvious point, is the default _ (underscore) variable. Not $_, just plain "_". When you do a file test operation, all this "stat" info gets collected via a file system request. If you want another file test on the same file, (like -s,-f,-d), using the "_" variable means to use use previous stat info without making another expensive call to the file system.

Hope this at least provides some fuel for thought and further improvement.

#!/usr/bin/perl -w use strict; use File::Find; # find() calls the wanted subroutine for each file and directory # underneath the starting point(s) (can specify more than one # directory tree to traverse down) find(\&wanted, "C:/temp"); # within subroutine "wanted": # use these variables to figure out where you are: # $File::Find::dir is the current directory name, # $_ is the current filename within that directory # $File::Find::name is the complete pathname to the file. # wanted() cannot return anything directly # declare a data struct at a higher scope that this sub # writes to my %mailboxesWithMessages; my %allMailboxes; sub wanted { #full path must contain a mailbox number my $mbox; return() unless ( ($mbox) = $File::Find::name =~ /(\d+)\@/); if (-d $File::Find::name) #some mbox may have no .msg files { $allMailboxes{$mbox}=1; return; } #must be looking at a messsage file return() unless ( -f _ and $File::Find::name =~ /\.msg$/); my $size = -s _; $size = 4096 if $size < 4096; $mailboxesWithMessages{$mbox}+= $size; # comment out above and run this sub with just this line to # see what it does... # print "$File::Find::name\n"; return; }
sort %mailboxesWithMessages to get the biggest one(s). Cycle thorough %allMailboxes - any key there that doesn't exist in the other hash means a mbox with no messages (empty).

Replies are listed 'Best First'.
Re^2: Optimizing performance for script to traverse on filesystem
by gdanenb (Acolyte) on Feb 02, 2012 at 09:55 UTC

    I tested the same logic in a way you suggested and found it a little bit slower for a big directories.
    But writing is much easier of course

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951317]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2017-09-25 01:02 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (275 votes). Check out past polls.