Reputation: 133
I am using File::Find
to go through a ton of files and then doing cleanup based on their size, age etc. I am accessing these files through windows network shares so the stat calls are expensive
The code looked something like this
sub findItems
{
my $item = $File::Find::name;
my $dir = $File::Find::dir;
my $itemName = $_;
if (-f $item) { .. }
elsif (-d $item) { .. }
my $age = -M $item;
my $size = -s $item;
Figured out that it was taking a lot of time. The culprit was the 3 calls -f/-d
, -M
and -s
, all needing a fresh stat
. So I changed it to
sub findItems
{
my $item = $File::Find::name;
my $dir = $File::Find::dir;
my $itemName = $_;
if (-f $item) { .. }
elsif (-d $item) { .. }
my $age = -M _;
my $size = -s _;
This almost halved the time as I went from 4 stat
calls to 2. I said 4 because I know there is one stat
call in File::find
as well, and 3 are in my code.
My question is can I rely on the stat
call in File::Find
and change my code to this
sub findItems
{
my $item = $File::Find::name;
my $dir = $File::Find::dir;
my $itemName = $_;
if (-f _) { .. }
elsif (-d _) { .. }
my $age = -M _;
my $size = -s _;
This will again halve the time it takes. I guess I will have to actually check the File::Find
code to see if there might be some other stat
call on anything other than $item
thanks
Upvotes: 4
Views: 117
Reputation: 22254
Based on its documentation, "It is guaranteed that an lstat has been called before the user's wanted() function is called. This enables fast file checks involving _. Note that this guarantee no longer holds if follow or follow_fast are not set."
So I'd suggest that, under any other circumstance, you cannot rely on it.
Upvotes: 3