I appreciate that swaroop prefers not to use Unix commands, and I've also noticed that using the stat operations should be platform agnostic provided that platform is supported in perl, where a Unix command would be tied to a Unix variant and would likely not work under, say, Windows (without cygwin or other variants).

However, given a restraint that says we will be used under Unix, is the overhead of a system call worse than stat'ing each and every file, which must at some point be converted to a system call as well? I would think that the `ls -lrt` option would have to be a more CPU time efficient operation.

Yeah, I should run a benchmark. I've resisted this for a long time, but there's no time like the present... I'll post an update when I get done; unless someone beats me to it.

-Scott

Update:

#!/usr/bin/perl use strict; use warnings; use List::Util 'reduce'; use Benchmark qw(cmpthese); our $path = shift || '/usr/bin/*'; sub a { my $newest = ( reduce { $a->[1] < $b->[1] ? $b : $a } map { [ $_, (stat)[9] ] } glob "$path" )->[0]; } sub b { my %file_date; for ( glob "$path" ) { $file_date{$_} = (stat)[9]; } my $newest = ( sort { $file_date{$b} <=> $file_date{$a} } keys %fi +le_date )[0]; } sub c { use List::Util 'reduce'; my $newest = reduce { (stat $a)[9] < (stat $b)[9] ? $b : $a } glob "$path"; } sub d { my %file_date; my $max = -99999999; # set it to first file's mtime would be bette +r # but just for demonstration here my $mtime; my $file; for ( glob "$path" ) { $mtime = (stat)[9]; if ($max <= $mtime) { $file = $_; $max = $mtime; } } my $newest = $file; } sub e { my @array = `ls -lrt $path`; my $newest = $array[-1]; } cmpthese(250, { zaxo_first => \&a, graff_hash => \&b, many_stats => \&c, sk_maxfile => \&d, the_ls_lrt => \&e, });

             Rate graff_hash zaxo_first many_stats sk_maxfile the_ls_lrt
graff_hash 25.3/s         --       -14%       -21%       -40%       -84%
zaxo_first 29.3/s        16%         --        -9%       -31%       -81%
many_stats 32.3/s        27%        10%         --       -24%       -79%
sk_maxfile 42.4/s        67%        45%        31%         --       -73%
the_ls_lrt  155/s       513%       430%       381%       266%         --
Ok, I think something's off, I'd expect the "many_stats" to be the worst performing option, but it turns out to be not so bad. This is run on our /usr/bin directory with 1643 files...

In reply to Re: how to list the files in dir with respect to time by 5mi11er
in thread how to list the files in dir with respect to time by swaroop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.