Monks

In my current program I wish the user to be given feedback as to the execution time of various sections of the program. As such I have broken the program into a series of logical operations, each contained within a subroutine. I am using the Benchmark module included with perl to measure execution time. Some of the subroutines do not require parameters and thus the following is fine:

use strict; use warnings; use Benchmark; sub stuff() { my $x = 1000000; foreach my $i (1..$x) { print "." } print "\n"; } my $t; $t = timeit(1, \&stuff); print "Timings: ", timestr($t), "\n";

However, some of the stages that I wish to time take parameters that are passed from the command line. the following code demonstrates the problem.

use strict; use warnings; use Benchmark; sub stuff($) { my $x = shift; foreach my $i (1..$x) { print "." } print "\n"; } my $t; $t = timeit(1, \&stuff(1000000)); print "Timings: ", timestr($t), "\n";

The code has no syntax errors and the subroutine runs. However, instead of outputting timings, once the sub has executed the following error is output:

Undefined subroutine &main::SCALAR called at (eval 2) line 1.

Is it possible for me to time code like this... or must I use a different approach. I could use the time function and just find the difference for each subroutine before and after, but this in inflexible and I would rather do it properly.

Thanks in advance,

____________
Arun

In reply to Timing a Subroutine with Parameters by arunhorne

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.