Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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?

Replies are listed 'Best First'.
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?

        There is a module that can get you high resoltion time stamps: Time::HiRes

        You should be able to take it from there.

        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'; }
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:

    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
Re: How can I keep track of the execution time of a script?
by awohld (Hermit) on Aug 05, 2016 at 06:52 UTC
    use Timer::Runtime

    #!/usr/bin/perl use Timer::Runtime; print "hi\n";
    $ perl Started: Fri Aug 5 01:48:14 2016 hi Finished: Fri Aug 5 01:48:14 2016, elapsed time = 00:00:0 +0.000398 $

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://35318]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2017-08-19 00:24 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.