Nice post. However, this example is a bit overdone ;-}. You use a Schwartzian trans-/deform logic. That's fine, as it effectively implements pope's cache. However, you can leave it out by doing the -M in sort.

I don't know how efficient perl's array/hash is versus the system calls. Let's try it!

#!/usr/bin/perl -w use strict; use Benchmark; opendir LOGS, "."; my @logs = readdir LOGS; my $cmp = timethese( -1, { 'Cached' => sub{ my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, -M $_] } # grep { /\.log$/ } @logs; }, 'Raw' => sub { my @sorted = sort { -M $a <=> -M $b } # grep { /\.log$/ } @logs; } } ); Benchmark::cmpthese( $cmp );
I had to comment out the greps and run it in /usr/bin just to get myself a decent amount of files (1965). Run under linux RH7 (.16 kernel)/ perl5.6/ ext2:
Benchmark: running Cached, Raw, each for at least 1 CPU seconds... Cached: 2 wallclock secs ( 0.90 usr + 0.15 sys = 1.05 CPU) @ 18 +.10/s (n=19) Raw: 2 wallclock secs ( 0.45 usr + 0.64 sys = 1.09 CPU) @ 8 +.26/s (n=9) Rate Raw Cached Raw 8.26/s -- -54% Cached 18.1/s 119% -- #that's too little iterations.... Benchmark: timing 1000 iterations of Cached, Raw... Cached: 105 wallclock secs (44.91 usr + 7.74 sys = 52.65 CPU) @ 1 +8.99/s (n=1000) Raw: 258 wallclock secs (47.15 usr + 78.43 sys = 125.58 CPU) @ + 7.96/s (n=1000) Rate Raw Cached Raw 7.96/s -- -58% Cached 19.0/s 139% --
As could be expected, the caching is more efficient, but the raw is only two times slower. All the extra time is spend on system time, the usr (perl) time is fairly indifferent.

It might have something to do with the other jobs my box is running (quite memory/ disk demandings ones...) I will update after those are finished. Update: Doesn't matter. Same results on the system idling.

I'm curious about other setups ... So if anyone'd like to run them....

Cheers,

Jeroen


In reply to Re:{4} Is too little too much? Coding under the microscope... by jeroenes
in thread Is too little too much? Coding under the microscope... by snafu

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.