While this is short, sorting all files by date only to get at the newest one is a waste, specially for large directories. Also, it does a -M at each comparison in sort.$report = (sort{-M $a <=> -M $b}<*>)[0];
Using the Schwartzian Transform
eliminates multiple -M calls, but uses more memory, since we build an anonymous array for each file. It appears that this linear approach$report = ( map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [-M $_, $_] } <*>)[0] );
is a cheap way, and it's faster. benchmarking gives$report = do { local *D; opendir(D,"."); my $t = time; my $ret; while(my $f = readdir(D)) { next if $f =~ /^\.\.?$/; my $ft = -M $f; if($ft < $t) { $ret = $f; $t = $ft; } } closedir(D); $ret; } ;
Benchmark: timing 1000 iterations of do, golf, st... do: 8 wallclock secs ( 4.00 usr + 4.21 sys = 8.21 CPU) @ 12 +1.80/s (n=1000) golf: 74 wallclock secs (20.15 usr + 53.05 sys = 73.20 CPU) @ 13 +.66/s (n=1000) st: 34 wallclock secs (18.96 usr + 14.01 sys = 32.97 CPU) @ 30 +.33/s (n=1000) Rate golf st do golf 13.7/s -- -55% -89% st 30.3/s 122% -- -75% do 122/s 792% 302% --
running on my /usr/share/man/man1 directory. Note the big difference in CPU time. -M is 40% faster than (stat)[9], no wonder, it does look only at modification time and doesn't build a list. Benchmarking the call to ls in backticks doesn't make sense, because perl is blocked during the ls run.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
In reply to Re: grab newest file
by shmem
in thread grab newest file
by hasimir44
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |