[PLUG] Grab the most recent file in perl?
Randal L. Schwartz
merlyn at stonehenge.com
Fri Feb 14 06:31:01 UTC 2003
>>>>> "Felix" == Felix Lee <felix.1 at canids.net> writes:
Felix> 'ls -t' will call stat() on each file just once, so for a
Felix> million files that's a million I/O operations. in contrast,
Felix> 'sort {-T $a <=> -T $b} <*>' for a million files will call
Felix> stat(), umm, I think it's somewhere over 4 million times.
Felix> I'd have to go looking for the exact formula. so if your
Felix> directory is NFS-mounted from geosynchronous orbit, then
Felix> that perl sort will take about 2 months longer to run.
So, you either use a Schwartzian Transform, effectively turning
Perl into a "fetch the mtime once" mode just like "ls -t", or
you use a high-water-mark algorithm:
my @files = <some*glob*here>;
my $oldest = shift @files;
my $age_of_oldest = -M $oldest;
for (@files) {
my $age = -M $_;
if ($age > $age_of_oldest) {
$oldest = $_;
$age_of_oldest = $age;
}
}
There. Single O/S request per file. Just like ls -t. Faster
than ls -t for a large list, because it's O(n), not a sort.
It's not Perl's fault that there are bad Perl programmers.
Felix, I'm no longer sure whether you're malicious, or just a bad
programmer. The jury remains out.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
More information about the PLUG
mailing list