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/
|