Perrin Harkins <[EMAIL PROTECTED]> said something to this effect on 10/29/2001:
> File::Spec has some handy methods for this kind of thing, which
> have the added benefit of being platform-independent.
OK, I've looked into it. Below, _inc_path2 is the one I posted
yesterday, and _inc_path1 is the equivalent File::Spec-based
subroutine. Here are my benchmark results (perl 5.6.1 on a
lightly loaded 600 MHz PIII, 128 M of RAM, running linux 2.2.18):
$ cat /tmp/filespec.pl
#!/usr/bin/env perl
use File::Spec;
sub _inc_path1 {
my $f = shift;
my @f;
for (File::Spec->splitdir($f)) {
# This next line doesn't work, for some reason
# push @f, File::Spec->canonpath(File::Spec->catfile($f[-1], $_));
push @f, File::Spec->canonpath("$f[-1]/$_");
}
return reverse @f;
}
sub _inc_path2 {
my $f = shift;
my %uniq;
my @dir;
local $" = '/';
return [
sort { length $b <=> length $a } # reverse sorted by length
grep { ++$uniq{$_} == 1 } # (unique directories only)
map { push @dir, $_; "/@dir"; } # a growing list of dirs
($f =~ m:([^/]+)/:og) # gathered from the current
]; # translated filename
}
use Benchmark;
my $f = '/usr/local/apache/htdocs/manual/index.html.en';
timethese ( 100_000, {
'_inc_path1' => sub { _inc_path1($f) },
'_inc_path2' => sub { _inc_path2($f) },
});
__END__
$ /tmp/filespec.pl
Benchmark: timing 100000 iterations of _inc_path1, _inc_path2...
_inc_path1: 40 wallclock secs (38.83 usr + 0.01 sys = 38.84 CPU) @ 2574.67/s
(n=100000)
_inc_path2: 15 wallclock secs (14.52 usr + 0.00 sys = 14.52 CPU) @ 6887.05/s
(n=100000)
A few runnings give the similar results.
(darren)
--
The limits of my language mean the limits of my world.
-- Ludwig Wittgenstein