Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Timing a process

by ukndoit (Sexton)
on Jan 31, 2004 at 05:41 UTC ( #325509=perlquestion: print w/replies, xml ) Need Help??
ukndoit has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl and am trying to make a script which will get the starting time, and the ending time, and then tell me how long it took to run the whole thing, ending subtract beginning = how many hours, minutes and seconds...

I have figured out I could start it like this,
my $beginning_time = time(); # Then run all my commands and stuff... my $ending_time = time();

So, if I did that, and then subtracted the ending from the begging, how would I turn that into x amount of hours, x amount of minutes, and x amount of seconds?

Please forgive my ignorance. I'm trying to learn, but I'm teaching myself. I've read a lot, but cannot figure this out. I'm very tired, I guess that could be causin it.

Anyways, thank you much for filling me in.


Replies are listed 'Best First'.
Re: Timing a process
by BrowserUk (Pope) on Jan 31, 2004 at 09:44 UTC

    My favorite tool for this is Benchmark::Timer.

    use Benchmark::Timer; my $T = new Benchmark::Timer; $T->start( 'Outer' ); for( 1 .. 1000 ) { $T->start( 'Inner' ); # Request a 50 millisec delay to simulate command select undef, undef, undef, 0.05; $T->stop( 'Inner' ) }; $T->stop( 'Outer' ); $T->report; 1 trial of Outer (199.094s total) 1000 trials of Inner (62.500s total), 62.500ms/trial

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!

Re: Timing a process
by powerhouse (Friar) on Jan 31, 2004 at 06:04 UTC
    Try Time::Duration
    That should do just what you need.

    If you don't have root access, you should have your server admin install it for you.

    Have fun in learning :o)

Re: Timing a process
by CountZero (Bishop) on Jan 31, 2004 at 08:34 UTC
    More general, if you want to time and benchmark your code, have a look at Benchmark.


    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Timing a process
by crabbdean (Pilgrim) on Jan 31, 2004 at 06:18 UTC
    You can also try using the Date::Calc module. It allows you to do all sorts of date calculations.

Re: Timing a process
by stvn (Monsignor) on Jan 31, 2004 at 14:20 UTC

    This is also relatively easy to accomplish without moudules (although modules are easier since the hard stuff is already done). But in the interest of learning more about perl.....

    time() returns the seconds from the epoch (Jan 1, 1970 on most systems). So $begining_time - $end_time will give you the number of seconds it took your script to run. From there, getting the hours, minutes, seconds is really just a matter of doing some math.

    Here is a quick one:

    $total_time = $end_time - $begining_time; # get the total minutes $minutes = ($total_time / 60); # get the reminader for seconds $seconds = ($total_time % 60); # get the total hours $hours = ($minutes / 60); # and re-adjust minutes to be just the minutes $minutes = ($minutes % 60); # I use printf here to make sure we have integers printf "%d hours, %d minutes, %d seconds", $hours, $minutes, $seconds;

Re: Timing a process (simple maths)
by grinder (Bishop) on Jan 31, 2004 at 13:57 UTC
    how would I turn that into x amount of hours, x amount of minutes, and x amount of seconds

    Your approach is quite sound for any process that is going to take many seconds to complete. The Benchmark module is good for measuring different snippets of code that take milliseconds to run.

    If your question is simply how to convert a large number of seconds into seconds, minutes, hours and beyond, you just have to take the modulo, and then divide by, the length of the interval you're interested in. The following code demonstrates the idea:

    my $seconds = $duration % 60; $duration /= 60; my $minutes = $duration % 60; $duration /= 60; my $hours = $duration % 24; my $days = $duration;

    That's only a rough sketch. You also have to deal with the rounding of the different division operations. It's much easier to get a module like Time::Duration to do it. You might also want to look at an old node of mine: Formatting elapsed time.

Re: Timing a process
by rcaputo (Chaplain) on Jan 31, 2004 at 19:45 UTC

    There's always time(1) if you're using a reasonable operating system:

    2) eyrie:~/perl/poe% time sleep 1
    sleep 1  0.00s user 0.01s system 0% cpu 1.041 total

    -- Rocco Caputo - -

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-05-27 16:54 GMT
Find Nodes?
    Voting Booth?