Cwd
module instead of calling pwd
repeatedly.Boy, it's not kidding. I wrote a test script that does almost nothing but change directories and print the current directory, repeating that about 7500 times. Using
`pwd`
to get the directory took an average wall-clock time of 11.7 seconds. If instead you import Cwd
's version of chdir()
, the script can use $ENV{PWD}
, which took an average wall-clock time of 0.3 seconds.Oddly, using
Cwd::cwd()
took an average of 15.4 seconds. This wasn't a very scientific test, but those numbers bore up under repeated tries (doing several runs in a row, throwing out the time for the first run). I used wall-clock time, because the sys/user time reported by Unix time
was quite a bit lower, even though it was an unloaded system. Maybe because the directories were NFS mounts.So here's a good strategy for using
Cwd
:
use Cwd qw(chdir);
sub cwd {
return $ENV{PWD};
}
Note: the
Cwd
documentation points out that $ENV{PWD}
is only kept up to date if every module used in the script uses Cwd::chdir
to change directories.