See the
Benchmark docs. It factors out the null loop, so even looping within your subroutine adds operations that you'd rather not time (for '&' and '%' anyway; if you are timing more expensive operations/algorithms then it is not significant). See this:
#!/usr/local/bin/perl -w
use strict;
use Benchmark;
my $i = 34;
timethese(3000000, {
AND=>\&and_them,
MOD=>\&mod_them,
NULL_LOOP=>\&null_loop,
});
sub null_loop {
}
sub and_them {
$i & 1;
}
sub mod_them {
$i % 2;
}
~
"tst" 23 lines, 229 characters
[rover:DEV]~/tst>./tst
Benchmark:
timing 3000000 iterations of
AND, MOD, NULL_LOOP
...
AND: 1 wallclock secs ( 1.68 usr + 0.00 sys = 1.68 CPU) @ 17
+85714.29/s (n=3000000)
MOD: 2 wallclock secs ( 2.33 usr + 0.00 sys = 2.33 CPU) @ 12
+87553.65/s (n=3000000)
NULL_LOOP: -1 wallclock secs (-0.07 usr + 0.00 sys = -0.07 CPU) @ -4
+2857142.86/s (n=3000000)
(warning: too few iterations for a reliable count)
(I realize that in the end it really doesn't matter, I'd probably use '%' anyway, and this is all just for the sake of discusssion/fun/curiousity).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.