Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Run a script multiple times and store the running time

by ww (Archbishop)
on Oct 14, 2016 at 02:09 UTC ( #1173977=note: print w/replies, xml ) Need Help??


in reply to Run a script multiple times and store the running time

Alternate approach but leaving the complexities that would cause varying execution times for the tested script, foo_a.pl (that's left as the proverbial challenge for the OP). Note also the caveat at LN9 of the script immediately below:

#!/usr/bin/perl -w use strict; use Time::HiRes qw[ time ]; # OP: "I have a perl script that I need to run 1000 times and for each + run # I need to store how much time it took to run... " # using foo_a.pl, execute this from foo_a's dir # Timings of foo_a.pl are NOT precise because they include the time # to execute the calculations here. my $begin = time; for my $arg( 1..1000 ) { my $start = time; system "perl foo_a.pl $arg"; my $duration = (time - $start); print "\n\t\$arg: $arg, \t\$start: $start, \t\$duration: $duration +\n"; $start = time; } print "$begin" . time . "\n"; exit;

foo_a.pl, an (almost) empty script:

#! /usr/bin/perl -w use strict; #foo_a.pl print "--foo_a.pl's marker inserted when run at LN11 in 1173964_a.pl: +"; exit;

And the output of D:\_Perl_\PMonks>D:\_Perl_\PMonks\1173964_a.pl > 1173964_a_OUT.pl on (Win7, AS 5.18), edited as noted:

--foo_a.pl's marker inserted when run at LN11 in 1173964_a.pl: $arg: 1, $start: 1476409956.16692, $duration: 0.0165810585 +021973 --foo_a.pl's marker inserted when run at LN11 in 1173964_a.pl: $arg: 2, $start: 1476409956.18353, $duration: 0.0134079456 +329346 --foo_a.pl's marker inserted when run at LN11 in 1173964_a.pl: $arg: 3, $start: 1476409956.19695, $duration: 0.0137300491 +333008 --foo_a.pl's marker inserted when run at LN11 in 1173964_a.pl: $arg: 4, $start: 1476409956.2107, $duration: 0.01424980163 +57422 # elided for brevity and henceforward, "marker inserted" lines deleted +: $arg: 495, $start: 1476409962.28221, $duration: 0.01291704 +17785645 $arg: 496, $start: 1476409962.29514, $duration: 0.01339387 +89367676 $arg: 497, $start: 1476409962.30855, $duration: 0.01174592 +97180176 $arg: 498, $start: 1476409962.32031, $duration: 0.01395082 +47375488 $arg: 499, $start: 1476409962.33427, $duration: 0.01355600 +35705566 $arg: 500, $start: 1476409962.34784, $duration: 0.01249790 +19165039 $arg: 501, $start: 1476409962.36035, $duration: 0.01373386 +38305664 $arg: 502, $start: 1476409962.3741, $duration: 0.012020826 +3397217 $arg: 503, $start: 1476409962.38613, $duration: 0.01191496 +84906006 $arg: 504, $start: 1476409962.39807, $duration: 0.01241683 +95996094 $arg: 505, $start: 1476409962.4105, $duration: 0.012879133 +2244873 # another long stretch omitted... $arg: 995, $start: 1476409968.50899, $duration: 0.01135301 +58996582 $arg: 996, $start: 1476409968.52035, $duration: 0.01180887 +22229004 $arg: 997, $start: 1476409968.53218, $duration: 0.01137590 +4083252 $arg: 998, $start: 1476409968.54356, $duration: 0.01149606 +70471191 $arg: 999, $start: 1476409968.55507, $duration: 0.01226401 +32904053 $arg: 1000, $start: 1476409968.56735, $duration: 0.0137550 +830841064

NB: initial "begin time": 1476409956.16692; time completed: 1476409968.58113 or roughly 12 seconds for 1000 iterations of foo_a.pl measured this way ... and yes, I see the command line is unnecessarily redundant... but so (perhaps) is this node.



If you didn't program your executable by toggling in binary, it wasn't really programming!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1173977]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-06-22 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (121 votes). Check out past polls.

    Notices?