Re: timing a command execution with perl
by LanX (Saint) on Dec 09, 2014 at 14:33 UTC
|
You can use Time::HiRes to get time within Perl execution.
But please keep in mind that the OS produces loads and overheads due to multitasking which will add to execution time.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
| [reply] |
Re: timing a command execution with perl
by fishmonger (Chaplain) on Dec 09, 2014 at 14:27 UTC
|
Why would you want to install/use Time::HR? That module hasn't been maintained since 2002.
Instead, you should look at using Time::HiRes, which is a core module.
| [reply] |
|
|
Ok, I installed the Time::HiRes module but how can I use it, it seems ot me a bit complicated.
In addition, I'm working at a real-time work. I have to be as much precise as I can.
| [reply] |
|
|
| [reply] |
Re: timing a command execution with perl
by james28909 (Deacon) on Dec 09, 2014 at 15:03 UTC
|
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";
| [reply] [d/l] |
|
|
$ 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.
| [reply] [d/l] [select] |
|
|
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? | [reply] [d/l] |
|
|
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.
| [reply] [d/l] |
|
|
| [reply] |
|
|
Is there some options to visualise also microseconds and milliseconds and not only seconds?
| [reply] |
|
|
| [reply] |
Re: timing a command execution with perl
by Anonymous Monk on Dec 09, 2014 at 14:53 UTC
|
| [reply] |
|
|
Indeed I used it in my perl program:
system("time", "program-name", "file-name");
but the out is different (and confused) with respect to that right output I have just typing
time program-name file-name
in the shell.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |