in reply to grab newest file

I'd do this with glob, -M and sort:
$report = (sort{-M $a <=> -M $b}<*>)[0];
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.

Using the Schwartzian Transform

$report = ( map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [-M $_, $_] } <*>)[0] );
eliminates multiple -M calls, but uses more memory, since we build an anonymous array for each file. It appears that this linear approach
$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; } ;
is a cheap way, and it's faster. benchmarking gives
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}

Replies are listed 'Best First'.
Re^2: grab newest file
by jwkrahn (Abbot) on Jul 07, 2006 at 11:40 UTC
    -M is 40% faster than (stat)[9], no wonder, it does look only at modification time and doesn't build a list.
    It doesn't build a list but it stores all the values from stat. From the -X man page:
    If any of the file tests (or either the "stat" or "lstat" operators) are given the special filehandle consisting of a solitary underline, then the stat structure of the previous file test (or stat operator) is used, saving a system call.
    So when you use -M _ or -f _ you are retrieving the stored values from the previous stat or file test.

      Right.

      If I say (stat)[9] I am building a list containing what stat returns and reference the element with index 9 in that list. If I use -M I don't create and operate on a list (although there's an underlying structure, of course).

      Getting the right element from the underlying structure directly is faster than pulling them all out and throwing away all elements but one (even though -M involves time delta calculation).

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