in reply to •Re^3: Why does File::Find chdir?
in thread Why does File::Find chdir?
Ah, yeah .. good point.
For those who want to prove this to themselves, here's the script I used to convince myself...
#!/usr/local/bin/perl # # command line arg negative: build test dirs that deep # command line arg positive: test that many iterations use Benchmark qw(cmpthese); use File::Find; sub build { # build up our testing directory recursively my ($dir, $depth) = @_; chdir $dir; if (0 < --$depth) { mkdir 'a'; build('a', $depth); } `touch aa`; foreach (0..1) { `touch $_`; } foreach (5..6) { if (1 < rand 2) { `ln -s aa $_`; } if (1 < rand 2) { `ln aa $_$_`; } } chdir '..'; } my $wanted = sub { if (-l) { # it's a symlink my ($dev, $ino) = lstat _; # reuse info from -l push @{$results{"$dev $ino"}}, $File::Find::name; if (-e) { # that points somewhere else my ($dev, $ino) = stat _; # reuse info from -e push @{$results{"$dev $ino"}}, "symlink:$File::Find::name"; } } else { my ($dev, $ino) = stat; push @{$results{"$dev $ino"}}, $File::Find::name; } }; my $arg = shift || 2; if (0 > $arg) { build('.', -$arg); } else { my %results; cmpthese($arg, { chdir => sub { %results=(); find { wanted=>$wanted }, '.'; }, no_chdir => sub { %results=(); find { wanted=>$wanted, no_chdir=>1}, '.'; } }); } __END__
And the result...
laptop:~/tmp/monk-test> ~/monk.pl -50 laptop:~/tmp/monk-test> ~/monk.pl 100 Benchmark: timing 100 iterations of chdir, no_chdir... chdir: 2 wallclock secs ( 1.39 usr 0.40 sys + 0.12 cusr 0.10 +csys = 2.01 CPU) @ 55.87/s (n=100) no_chdir: 3 wallclock secs ( 1.50 usr 1.08 sys + 0.09 cusr 0.17 +csys = 2.84 CPU) @ 38.76/s (n=100) Rate no_chdir chdir no_chdir 38.8/s -- -31% chdir 55.9/s 44% -- laptop:~/tmp/monk-test> ls 0 1 a aa laptop:~/tmp/monk-test> rm -rf * laptop:~/tmp/monk-test> ~/monk.pl -100 laptop:~/tmp/monk-test> ~/monk.pl 100 Benchmark: timing 100 iterations of chdir, no_chdir... chdir: 4 wallclock secs ( 2.98 usr 0.71 sys + 0.13 cusr 0.12 +csys = 3.94 CPU) @ 27.10/s (n=100) no_chdir: 8 wallclock secs ( 3.03 usr 3.98 sys + 0.13 cusr 0.21 +csys = 7.35 CPU) @ 14.27/s (n=100) Rate no_chdir chdir no_chdir 14.3/s -- -47% chdir 27.1/s 90% --
|
---|