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

In reply to Re^4: Why does File::Find chdir? by hossman
in thread Why does File::Find chdir? by jplindstrom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.