Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Timing Your Program

by VicBalta (Scribe)
on Oct 01, 2001 at 21:54 UTC ( [id://115923]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

My question that I am wondering about is timing your program. I am writing a program and I am have a couple of different design options that I am looking at. Some of it is wether or not I am to use fork in my program, and how that will effect the time it takes. But another factor is what will be the lag time over doing things over the network. I Know that I can use time to get the current time (in a 7 digit format i think). But I want to know if there is some kind of stop watch module I can use so that I can start more than one stop watch to compare what part takes what amount of time. If any one knows any thing or has some good examples please reply.

thank you

Replies are listed 'Best First'.
Re: Timing Your Program
by Moonie (Friar) on Oct 01, 2001 at 22:11 UTC
Re: Timing Your Program
by lhoward (Vicar) on Oct 02, 2001 at 00:21 UTC
    You may also want to check out the Benchmark module.
      Note that it is part of the perl core (as of perl5.003_07).

      It does just what you want, ie. allows you to compare the execution times of different approaches.

      use Benchmark; my $t0 = new Benchmark; # See code run my $t1 = new Benchmark; my $td = timediff($t1, $t0); print timestr($td) . "\n";
      Use as many or as few of those Benchmark objects as you'd like at any point in the program. The CPAN docs on Benchmark are great, but I thought it may be helpful to see the code right here. Instant gratification is key. :)
        what module does Benchmark come from?
Re: Timing Your Program
by scain (Curate) on Oct 02, 2001 at 00:05 UTC
    Certainly on unix-like systems, there is the time command, with simple syntax like
    % time myScript.pl
    which will execute the program and when it is done, it will print the amount of system and real time it used. One possible problem to consider when doing timings concurrently is competion for resources, i.e., is one script getting more cpu cycles than the other.

    Scott

    Ugh! Try reading the whole question scain! Sorry 'bout that.

Re: Timing Your Program
by cjensen (Sexton) on Oct 02, 2001 at 00:47 UTC
    You might look at Time::HiRes which implements hi resolution versions of time, alarm and sleep. Get and store the time of day in floating seconds (or seconds and microseconds) at whatever parts of your script it makes sense to watch. At the end, use the supplied tv_interval subroutine to compute the differences between two fractional times. I use this in conjunction with Apache::Scoreboard to do some near real-time monitoring of high traffic web servers, attempting to subtract the time it takes to execute commands and receive scoreboard information from a fixed interval so that the timing and calculations are more accurate. It works well.
Re: Timing Your Program
by Spudnuts (Pilgrim) on Oct 02, 2001 at 17:33 UTC
    Would this work for you:
    % perl -d:DProf mycode.pl % dprofpp
    It is documented in chapter 20 of Camel 3.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found