Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How can I keep track of the execution time of a script?

by Sarcasmo (Initiate)
on Oct 04, 2000 at 23:31 UTC ( #35318=perlquestion: print w/ replies, xml ) Need Help??
Sarcasmo has asked for the wisdom of the Perl Monks concerning the following question:

I am researching some things for an upcoming project at work, and I've been commissioned to find a way to keep track of how long code takes to execute. Why? The first example I showed to the person heading the project took forever to load because of his input. It's a form (I'm showing him that I can use the CGI module) that inputs a string in a text field, and then ravages it with regular expressions and random characters to make, basically, a big mess that's pretty. If you input a string without spaces (as he did), it takes longer to execute. I want to meter how long it takes and (I guess) output the run time.

So yeah, if anyone can tell me how to benchmark a script without using any modules (not counting CGI), please do. I've looked around and can't really find a good way to do it. To be honest, I don't even really have an idea...

Comment on How can I keep track of the execution time of a script?
RE: How can I keep track of the execution time of a script?
by Adam (Vicar) on Oct 04, 2000 at 23:39 UTC
    $^T is the time that the script started. So end your script by returning time()-$^T. You might want to put this in the sig die handler:
    # your script goes here BEGIN { $SIG{__DIE__} = sub { print + time() - $^T, $/, @_ } } print + time() - $^T, $/
    That prints the run time of your script in seconds.

    Update: By the way, there is a module called Benchmark that is great for testing algorithms and routines and such. Turnstep wrote a great tutorial on it called Benchmarking Your Code

    Update: For a variety of reasons, it is much better (and simpler too!) to use a single END block instead of the repition that I suggested. Thanks merlyn for pointing this out. (I also like the use of warn)

    Also, If your script is taking longer then a minute or two, you might want to break down those seconds to be minutes:seconds, but don't forget the more work you do after that last call to time, the less accurate the stamp. (Well, time only has a granularity of a second... so you actually have plenty of room, but anyway...)

    END { $a=time-$^T, warn sprintf "Runtime %d min %d sec\n", $a/60, $a%6 +0 }
      Perhaps you wanted an END block instead of a DIE handler.
      END { warn "this script took ", time - $^T, " seconds\n"; }
      A die handler is only triggered on aborts. END blocks are triggered on all normal terminations, including aborts.

      -- Randal L. Schwartz, Perl hacker

        Good call... I forgot about the END block.
      Thanks. That works nicely. How can I get that in milliseconds?

        If you're running this script in Windows you can use the Win32::GetTickCount to get the time in miliseconds I would do something like this:
        BEGIN { $ms = Win32::GetTickCount(); } END { warn 'This script took ',Win32::GetTickCount() - $ms,'miliseconds'; }
        There is a module that can get you high resoltion time stamps: Time::HiRes

        You should be able to take it from there.

Re: How can I keep track of the execution time of a script?
by arturo (Vicar) on Oct 04, 2000 at 23:46 UTC

    Many *nices also have the nifty 'time' utility, which you execute from the command-line like so:

    time foo.pl
    Of course, if you could use Benchmark you could time individual sections of code, which would be helpful, but sigh ... (I'm pretty sure it is in the standard dist)

    Philosophy can be made out of anything -- or less

RE: How can I keep track of the execution time of a script?
by little (Curate) on Oct 05, 2000 at 00:42 UTC
    my ($sec,$min,$hour,@garbage) = gmtime(time - $^T); my $runningtime = sprintf("%02d:%02d:%02d", $hour, $min, $sec);
    actual time at this valuation minus the special Perl VAR holding the scripts invocation time gives back $runningtime as a string "hh:mm:ss"
    I know, it's quick and dirty and not good enough for benchmarking, but to give you something to compare :-)
    Have a nice day
    All decision is left to your taste

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (15)
As of 2014-10-01 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (34 votes), past polls