in reply to How can one best measure compile time of their Perl script?

G'day Chris,

Perhaps you're looking for something like this:

#!/usr/bin/env perl -l BEGIN { use Time::HiRes 'time'; $::t0 = time; print "BEGIN: $::t0"; } use strict; use warnings; CHECK { $::t1 = time; print "CHECK: $::t1"; print 'DIFF: ', $::t1 - $::t0; } INIT { $::t2 = time; print "INIT: $::t2"; } END { $::t3 = time; print "END: $::t3"; print 'DIFF: ', $::t3 - $::t2; print 'Total: ', $::t3 - $::t0; }

Obviously, that won't include use Time::HiRes 'time'; in the timings: that might be a good thing. Here's a couple of sample runs:

BEGIN: 1385861138.48632 CHECK: 1385861138.48653 DIFF: 0.000216007232666016 INIT: 1385861138.48655 END: 1385861138.48658 DIFF: 2.40802764892578e-05 Total: 0.000259876251220703
BEGIN: 1385861139.78308 CHECK: 1385861139.78329 DIFF: 0.000212907791137695 INIT: 1385861139.78331 END: 1385861139.78333 DIFF: 2.38418579101562e-05 Total: 0.00025486946105957

BEGIN to CHECK is the compile time; INIT to END is the execution time: see "perlmod: BEGIN, UNITCHECK, CHECK, INIT and END" if you're unfamiliar with these.

Earlier responses have pointed out that "times vary": I'll assume you've understood the issues here. With substantially more compilation/runtime processing (than I've shown here) you may start to see more consistent results.

You haven't indicated what you want to use this for. Averaging results from multiple runs, at different times of the day, or under different loads, would probably be more useful than taking individual snapshots and trying to infer whatever from them.

-- Ken

Replies are listed 'Best First'.
Re^2: How can one best measure compile time of their Perl script?
by taint (Chaplain) on Dec 01, 2013 at 03:50 UTC
    Thanks for your response, and your example, kcott.

    Indeed, I understand the "varying times" situation. While I'm fairly familiar with perlmod. I appreciate your pointing those sections out.

    Reason for asking; is that I had an inclination to add a timer to a page template I had just created, and wanted to see how it performed, compared to the one I had been using. It occurred to me, given that there are so many different Time related Modules available. Is/would there be a better one than the one(s) I had chosen. If so. How would I know (without a great deal of bench testing). It all seemed so impractical. Then, as I thought how one should be implemented. I figured, surely I'm not the only one that has had to deal with this. So I searched PerlMonks. Then based on the results. Decided to see if I could discover, or reach some consensus on this.

    I think, given the input you, and others have provided:

    • It simply isn't possible to obtain a more accurate results, without a few millisecond deviation.
    • Consensus can only be obtained via averaging; and even then results will differ from OS to OS.
    Thank you again, kcott for your helpful reply.

    --Chris

    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;