Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I've spent the last few weeks optimizing and reoptimizing and re(n)optimizing a math module (Math::Combinator) I've been working on (to be released here soon), and had to wonder exactly how fast certain thing were, and how fast they were compared to each other. These things are the fundamental components of most programming languages: arrays, hashes, subroutines (by ref), sub's (by value). So, duh, Benchmark.
use Benchmark; $a = shift; $b = shift; print "A = $a\tB = $b\n"; $hash{$_} = $_ for (1..$b); @array[$_] = $_ for (1..$b); sub doSubR { ${$_[0]} = ${$_[0]}} sub doSubV { return $_[0] } timethese ( $a, { '1 Nothing ' => 'for (1..$b) { $trash = $_ };', '2 Mult ' => 'for (1..$b) { $trash = $_ * $_ };', '3 Array ' => 'for (1..$b) { $trash = $array[$_] };', '4 Hash ' => 'for (1..$b) { $trash = $hash{"$_"} };', '5 Sub (Ref)' => 'for (1..$b) { doSubR(\$_); $trash = $_ };', '6 Sub (Val)' => 'for (1..$b) { $trash = doSubV($_) };', });
For A = 1,000 and B = 10,000 I get these results, which I find fairly satisfying for accuracy:
[lexicon]$ perl bench.pl 10 1000000 A = 10 B = 1000000 Benchmark: timing 10 iterations of 1 Nothing , 2 Square , 3 Array + , 4 Hash , 5 Sub (Ref), 6 Sub (Val)... 1 Nothing : 35 wallclock secs (29.99 usr + 0.10 sys = 30.09 CPU) 2 Square : 58 wallclock secs (48.79 usr + 0.23 sys = 49.02 CPU) 3 Array : 63 wallclock secs (54.49 usr + 0.20 sys = 54.69 CPU) 4 Hash : 184 wallclock secs (156.99 usr + 0.60 sys = 157.59 CPU) 5 Sub (Ref): 388 wallclock secs (327.87 usr + 1.30 sys = 329.17 CPU) 6 Sub (Val): 312 wallclock secs (266.54 usr + 1.02 sys = 267.56 CPU)

Here's my interpretation of things (the Relative column translates to the number of multiplication operations that could be performed in the time it takes to perform the Function):

Function Time(S) Relative Mult: 19 01.00 Array: 24 01.25 Hash: 127 06.68 Sub (R): 299 15.70 Sub (V): 237 12.47

Question #1: Interpretation? My instinct is to subtract Nothing from the rest and use that as the actual base, thereby ignoring the overhead of Benchmark and the for loops.

Question #2: Those for loops: I want to get rid of cacheing in the hash and arrays. Is that a valid concern? Are the for loops a valid solution?

Question #3: Pass by reference is slower than Pass by value. Is that to be expected in this case? I thought bouncing things off the stack was supposed to be slow.

Question #4: What have I forgotten that completely invalidates my whole process?

-Lexicon


In reply to Fundamental Benchmarks by Lexicon

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (9)
    As of 2014-08-22 14:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (158 votes), past polls