in reply to Which

You seem to have the fastest solution. Unless I miss my guess, File::Find is too much for an operation like this:

#!/usr/bin/perl -w use strict; use Benchmark; use File::Find; my @paths = (split/\:/,$ENV{PATH}); sub which1 { my $filename= "test.txt"; foreach my $pathname (@paths) { print "$pathname/$filename\n" if ( -e "$pathname/$file +name" ); }; } sub which2 { my $filename = "test.txt"; finddepth sub {print "$_\n" if ( /$filename$/)} , @paths; } timethese (100, {'whichone' => \&which1, 'whichtwo' => \&which2});

Yields:

[10:27:15 jhorner@gateway scripts]$ ./ch0as-1.pl test.txt Benchmark: timing 100 iterations of whichone, whichtwo... whichone: 0 wallclock secs ( 0.01 usr + 0.02 sys = 0.03 CPU) @ 33 +33.33/s (n=100) (warning: too few iterations for a reliable count) whichtwo: 14 wallclock secs (12.54 usr + 1.55 sys = 14.09 CPU) @ 7 +.10/s (n=100)

Unless someone can tell me why the File::Find version takes so long, I'm stumped. I have a feeling it has to do with File::Find traversing the entire filetree under the @path directories. If someone can let me know how to make it search ONLY the @path directories and not subdirectories, I'll be grateful.

I also have a feeling that File::Find is overkill in this, since you aren't recursing the directory structure.

Anyone else have good info on this?

J. J. Horner
Linux, Perl, Apache, Stronghold, Unix
jhorner@knoxlug.org http://www.knoxlug.org/

Replies are listed 'Best First'.
RE: RE: Which
by buzzcutbuddha (Chaplain) on Jun 21, 2000 at 19:29 UTC
    I looked at the code for File::Find, and two things stand out immediately that I think make a big difference:
    • Code Size - File::Find is about 9 pages of code!
    • @ISA - Being OO, it uses CWD in it's @ISA, which is more code to load.
    Those two things alone will slow down execution compared to 3 lines of code making up a foreach loop.
    Of course, the File::Find makes up for it's size in it's flexibility and features.... :)

    hth, Maurice