in reply to timing a command execution with perl

Here is an example:
use strict; use warnings; use diagnostics; use Time::HiRes qw( time ); my $start = time(); ...code goes here... my $end = time(); my $runtime = sprintf("%.16s", $end - $start); print "This script took $runtime seconds to execute\n";

Replies are listed 'Best First'.
Re^2: timing a command execution with perl
by johngg (Canon) on Dec 09, 2014 at 15:40 UTC

    Using gettimeofday and tv_interval makes it a bit simpler.

    $ perl -Mstrict -Mwarnings -MTime::HiRes=gettimeofday,tv_interval -E ' my $t0 = [ gettimeofday() ]; my @psLines = qx{ ps -ef }; print qq{Command took }, tv_interval( $t0, [ gettimeofday() ] ), qq{ seconds to run\n};' Command took 0.015902 seconds to run $

    I hope this is helpful.

    Cheers,

    JohnGG

Re^2: timing a command execution with perl
by Anonymous Monk on Dec 09, 2014 at 15:38 UTC
    so I should write:
    use strict; use warnings; use diagnostics; use Time::HiRes qw( time ); my $start = time(); system("program-name file-name"); my $end = time(); my $runtime = sprintf("%.16s", $end - $start); print "This script took $runtime seconds to execute\n";
    isn't it?
      I mean something like:
      use strict; use warnings; use diagnostics; use Time::HiRes qw( time ); my $start = time(); stringsNthings(); your actual code goes here; whether it be system(whatever_prog, whatever_arg); or sub stringsNthings{ my $hello_world = "hello world\n"; my $sum = 1 + 2; chomp(my name = <STDIN>); } print stringsNthings($hello_world); print stringsNthings($sum."\n"); print stringsNthings($name); my $end = time(); my $runtime = sprintf("%.16s", $end - $start); print "This script took $runtime seconds to execute\n";
      As long as you put working code in there it should time it. The code posted more than likely wont work, its just a visual example.

      That measures the elapsed real time (wall clock time), not CPU time. Which do you want to measure?

Re^2: timing a command execution with perl
by Anonymous Monk on Dec 09, 2014 at 15:45 UTC
    Is there some options to visualise also microseconds and milliseconds and not only seconds?