So I'm finally getting into the habit of benchmarking while I'm coding and I run across this article online. I like it but I hate to type the entire function, so I figure I'll read it from a file, like so:
use Benchmark; my $code = ''; print "Enter number of code runs:\n"; my $runs = <STDIN>; chomp ($runs); print "Inline or load up code(1|2)?\n"; my $inline = <STDIN>; chomp($inline); if($inline == 1) { $/ = "#END"; print "Enter code block (end with #END):\n"; $code = <STDIN>; chomp($code); print "\n...Testing...\n"; timethis($runs, $code); } else { my $file; my $func; print "Give me the file name: \n"; $file = <STDIN>; chomp($file); $code = `cat $file`; eval($code); die $@ if $@; if($code=~m/sub\s+(\w+)/) { $func = $1; } no strict 'refs'; print "\n...Testing...\n"; timethis($runs, &$func); }
In case you are interested, here is the dummy function it is trying to run:
sub manic { my $x = 0; for (0..50) { $x++; } print $x; }
When I run this with the code option I get the following:
:)~> perl bmark.pl Enter number of code runs: 2000 Inline or load up code(1|2)? 2 Give me the file name: test.al ...Testing... 51timethis 2000: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) (warning: too few iterations for a reliable count)
See? It just ran one time. If I manually type in the manic function's code it will run all 2000 times. What gives?

Celebrate Intellectual Diversity


In reply to Benchmark Runs Only Once by InfiniteSilence

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.