in reply to The -d switch isn't working as I expected it to (test to see if its a directory)

If this is the actual code, then the problem is probably a typo ...

my ($startdir) = @_; chdir("$stardir\\") or die "Unable to enter dir $startdir:$!\n"; ^^^^^^^
Since your chdir isn't going where you want, and the names don't have paths prepended to them, the '-d' test will fail. FWIW rather than chdir I usually just prepend the directory name to the paths. This is less error prone since you don't have to remember to chdir back to the previous directory.
  • Comment on Re: The -d switch isn't working as I expected it to (test to see if its a directory)
  • Download Code

Replies are listed 'Best First'.
Re^2: The -d switch isn't working as I expected it to (test to see if its a directory)
by sgifford (Prior) on Jan 07, 2005 at 21:40 UTC
    Using chdir is about twice as fast:
    $ perl chdir_benchmar Benchmark: timing 1000 iterations of with_chdir, without_chdir... with_chdir: 16 wallclock secs ( 8.11 usr + 8.46 sys = 16.57 CPU) @ 60.35/s (n=1000) without_chdir: 31 wallclock secs (10.98 usr + 19.15 sys = 30.13 CPU) @ 33.19/s (n=1000)
      Not on my mac G5 with a directory of 10,000 files...

      Benchmark: timing 100 iterations of with_chdir, without_chdir... with_chdir: 25 wallclock secs ( 3.02 usr + 20.31 sys = 23.33 CPU) @ 4 +.29/s (n=100) without_chdir: 28 wallclock secs ( 3.76 usr + 21.65 sys = 25.41 CPU) @ + 3.94/s (n=100)

        Update: I misread bluto's numbers somehow and thought his was faster without chdir; re-reading, they're about what I would expect.

        Huh, that's hard to explain. What kind of filesystem is it? How deep of a directory is it?

        AFAIK, using chdir lets the OS check permissions on all parent directories only once, and after that it can just check permissions on each file. Using the full path requires that the OS look up the permissions information for parent directories much more frequently, and so is slower.

        I can see MacOS having a really good cache where chdir is no faster, but I don't understand why it would be slower...

        Update: On my wife's G4 with MacOS X 10.3.7 with a journaled MacOS Extended Filesystem on a directory 7 levels deep with 2688 files, I get:

        $ ~/tmp/t3 Benchmark: timing 500 iterations of with_chdir, without_chdir... with_chdir: 34 wallclock secs ( 4.39 usr + 12.34 sys = 16.73 CPU) @ 29.89/s (n=500) without_chdir: 62 wallclock secs ( 5.79 usr + 19.93 sys = 25.72 CPU) @ 19.44/s (n=500)