Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Run a script multiple times and store the running time

by Anonymous Monk
on Oct 13, 2016 at 21:59 UTC ( [id://1173964]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
I was wondering if I could get your valuable help on this matter : 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, for example, the time command in Unix.
What I was thinking to do is to create a file with 1000 commands, like:
time perl script.pl ARG1 time perl script.pl ARG2 .... time perl script.pl ARG1000

but I do not know how I can save the output of the time command somehow so that I can see it afterwards, that for running the script with the argument ARG1 it took say 1 min, then for ARG2 took 2 mins etc.
Any help would be grately appreciated!

Replies are listed 'Best First'.
Re: Run a script multiple times and store the running time
by BrowserUk (Patriarch) on Oct 13, 2016 at 22:07 UTC

    How about:

    #! perl -slw use strict; use Time::HiRes qw[ time ]; while( my $arg = <DATA> ) { my $start = time; system "perl script.pl $arg"; printf "With '%s' took: %.3f seconds\n", $arg, $time() - $start; } __DATA__ 1000 args here ... ...

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Just a minor correction here (I hope no offence is taken):
      It seems that :
       printf "With '%s' took: %.3f seconds\n", $arg, $time() - $start;
      should instead be:
      printf "With '%s' took: %.3f seconds\n", $arg, time() - $start;
      Aha, I will give it a try!
      Thank you!
Re: Run a script multiple times and store the running time
by ww (Archbishop) on Oct 14, 2016 at 02:09 UTC

    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!
Re: Run a script multiple times and store the running time
by Laurent_R (Canon) on Oct 14, 2016 at 06:19 UTC
    Just a wild guess from looking at your question: perhaps what you're really looking for is a module such as Benchmark.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1173964]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-24 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found