taint has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Monks.

I've often wondered how to accurately measure the compile/runtime of a Perl script. For example, I can imagine a web page would be the most difficult. Given the different Modules that might be used, Perl processing the text/data included, not to mention the included Modules. I've looked at Time::HiRes using

Time::HiRes qw/gettimeofday tv_interval/; my $start_time = [gettimeofday]; # AT end of page my $total_time = tv_interval($start_time); print $start_time
I've also looked at Time::Elapse, yet both yield different results, when using the exact same sample data (page). So I thought I might inquire here, and see if anyone might have a definitive answer, or solution.

Thanks for your time.

--Chris

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

Replies are listed 'Best First'.
Re: How can one best measure compile time of their Perl script?
by tobyink (Canon) on Nov 30, 2013 at 10:19 UTC

    I generally do:

    time perl -c script.pl
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Really? Has to be pretty big script to take more than a few millisec. For linux, there's a tool bundled with sources (linux/tools/perf). Far more precise and informational.

        "Really? Has to be pretty big script to take more than a few millisec."

        This one-liner seems to take more than a few milliseconds:

        time perl -c -e'use Moose; 1;'
        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
        Thanks, oiskuu.

        Nice output. While I do have stat, and lstat for that matter, and of course, perl. I don't have perf -- at least not by name. Probably have something by another name (I'm using *BSD).

        Which means, for the sake of portability, I should use something derived from Perl itself. Though
        perl -c
        seems to provide some interesting statistics. :)

        Thanks for the informative response, oiskuu.

        --Chris

        #!/usr/bin/perl -Tw
        use Perl::Always or die;
        my $perl_version = (5.12.5);
        print $perl_version;
Re: How can one best measure compile time of their Perl script?
by Anonymous Monk on Nov 30, 2013 at 07:56 UTC
      "Hmm....compile time, time compile, compile time,time compile, time compile ... hmmm ... meh Compile Phase Time"

      Appreciate that.

      "times vary
      I tried :)
      "

      I tried that too, and as you noted; "times vary". Which is what led me to ask "see if anyone might have a definitive answer, or solution."

      Thanks for trying. I appreciate it. :)

      --Chris

      #!/usr/bin/perl -Tw
      use Perl::Always or die;
      my $perl_version = (5.12.5);
      print $perl_version;
Re: How can one best measure compile time of their Perl script?
by Laurent_R (Canon) on Nov 30, 2013 at 19:50 UTC

    The problem with the Unix time command is that it is not very reliable for very short processes:

    $ time perl -c -e'use posix; 1;' -e syntax OK real 0m0.211s user 0m0.015s sys 0m0.077s ~ $ time perl -c -e'use posix; 1;' -e syntax OK real 0m0.093s user 0m0.015s sys 0m0.077s

      Interesting results, Laurent_R.

      Could it have anything to do with the scheduler (not sure Linux has that by name). But perhaps the Process Scheduler will be an issue regardless, and throw differing times. No matter what the size of the script.

      So, in the end. One can only count on an average. Not a precise result

      --Chris

      #!/usr/bin/perl -Tw
      use Perl::Always or die;
      my $perl_version = (5.12.5);
      print $perl_version;
        There may be some scheduling delay, indeed. Another possibility is the result of OS caching. It seems to take a longer time the first time you try it, and then varying, but smaller times, on the next runs. For example, on the above command, all the subsequent runs lasted between 0.080 and 0.120 second, only the first one was significantly longer. Then, there is of course the effect of other processes running on the platform, but in the case above, I was the only user on the box, and not running anything else significant at the same time.
Re: How can one best measure compile time of their Perl script?
by kcott (Archbishop) on Dec 01, 2013 at 01:53 UTC

    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

      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;