Yeah, it dosen't make sense to me either. I had already written the little one liner when someone here mentioned using an ST instead. For the exact same reason you stated above. I am checking the mod time of the same file at a minimum of 2 times, maybe more if the next file makes it out of the <=> operation.

Please keep in mind, this is the first time I have ever used an ST. It is pretty much copied directly from http://www.5sigma.com/perl/schwtr.html.

Here is the benchmark code I used to test it with the results. The directory had 16,108 files in it. Let me know what you think.

#!/usr/bin/perl -w use strict; use Benchmark; timethese(100, { 'chad' => \&chad, 'swartz' => \&st,}); sub chad { sort{ (-M $b) <=> (-M $a) } glob("*"); } sub st { map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, -M] } glob('*'); }

Here are the results of the benchmark:

Benchmark: timing 100 iterations of chad, swartz... chad: 118 wallclock secs (56.91 usr + 47.25 sys = 104.16 CPU) @ + 0.96/s (n=100) swartz: 1515 wallclock secs (142.33 usr + 1044.36 sys = 1186.69 CP +U) @ 0.08/s (n=100)

In reply to Re: Print the oldest file in a directory. by gnu@perl
in thread Print the oldest file in a directory. by gnu@perl

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.