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

Script execution time

by velusamy (Monk)
on Feb 04, 2009 at 12:18 UTC ( [id://741246]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I printed $^T variable to get the program execution time. But it prints the seconds since the epoch. I want to get the seconds from starting time of the script.

Could anyone tell me a way?

Replies are listed 'Best First'.
Re: Script execution time
by lakshmananindia (Chaplain) on Feb 04, 2009 at 12:24 UTC

    If you print $^T it won't print the execution time. It will print the time in epoch when the program started to execute. If you want that to print in human readable then

    use POSIX; print POSIX::strftime "%a %b %e %H:%M:%S %Y", localtime($^T); __END__ Wed Feb 4 17:45:10 2009

    Change the format to change the output.

Re: Script execution time
by gone2015 (Deacon) on Feb 04, 2009 at 14:41 UTC

    time() and $^T will be giving you elapsed real time, but in whole seconds.

    To get something with a finer grain, you should look at Time::HiRes. That provides:

    • Time::HiRes::time() which you can use instead of time() which will give whatever resolution your system will support -- it's reasonable to expect at least milli-seconds, and probably micro-seconds. You will have to do your own $Start_Time = Time::HiRes::time(), early on in the script, though. (Sadly, it does not seem to be possible to $^T = Time::HiRes::time().

    • you can also (Update: but not under Winders):

      use Time::HiRes qw(clock_gettime CLOCK_REALTIME) ; my $t = clock_gettime(CLOCK_REALTIME) ; # same as: my $t = Time::Hi +Res::time()

    • and if you want the cpu time, rather than the elapsed time, then:

      use Time::HiRes qw(clock_gettime CLOCK_PROCESS_CPUTIME_ID) ; my $cpu = clock_gettime(CLOCK_PROCESS_CPUTIME_ID) ;
      should give you cpu time since the process started. CLOCK_PROCESS_CPUTIME_ID is a POSIX thing, which is probably implemented on your system (Update: but not under Winders).

    Update: Time::HiRes::time() is supported on Winders. Unfortunately Time::HiRes::clock_gettime() isn't... Time::HiRes::clock() I find is also not implemented on Winders :-(

    However, testing on Linux and Windows XP, suggests that the following is supported by POSIX:

    use POSIX qw(CLOCKS_PER_SEC) ; my ($realtime, $user, $system, $cuser, $csystem) = POSIX::times() ;
    where this is retrning: the elapsed realtime since some point in the past (such as system startup); user and system times for this process; and user and system times used by child processes. They are in units of CLOCKS_PER_SEC, which on my XP is 1000 (so ms).

Re: Script execution time
by irah (Pilgrim) on Feb 04, 2009 at 12:24 UTC
    Ya, Using the following line, you can get. print time - $^T. It will print in seconds, ( the total execution time of the program ).
Re: Script execution time
by ww (Archbishop) on Feb 04, 2009 at 16:13 UTC

    By way of a method (excessively detailed which is also guilty of re-inventing a wheel) of getting a (low res, W32) execution time:

    #!C:\perl\bin\perl.exe -lw my $starttime = localtime(); my $starthms = $starttime; $starthms =~ /[A-Z]+\s[A-Z]+\s+\d+\s+(\d{2}:\d{2}:\d{2}).*/i; $starthms = $1; print $starthms; if ( $starthms =~ /(\d+):(\d+):(\d+)/ ) { my $hours = $1; my $minutes = $2; my $seconds = $3; our $startsecs = ($hours*60*60) + ($minutes*60) + $seconds; } sleep 2; # insert your code to be timed my $endtime = localtime(); my $endtimehms = $endtime; $endtimehms =~ /[A-Z]+\s[A-Z]+\s+\d+\s+(\d{2}:\d{2}:\d{2}).*/i; $endpart = $1; if ( $endpart =~ /(\d+):(\d+):(\d+)/ ) { local $hours = $1; local $minutes = $2; local $seconds = $3; our $endtimesecs = ($hours*60*60) + ($minutes*60) + $seconds; } print "\$endpart: $endpart and Runduration: " . ($endtimesecs - $start +secs) . " seconds"; print "=======================\n"; use POSIX; print "starttime, \$^T without POSIX: $^T"; print "Start time, , \$^T with POSIX: " . POSIX::strftime "%H:%M:%S", + localtime($^T); print "\t2nd POSIX, \$^T: " . POSIX::strftime "%H:%M:%S", localtime($^ +T); print "\t3rd POSIX, current time: " . POSIX::strftime "%H:%M:%S", loca +ltime(); print "local_time(): " . localtime();

    Upon execution,

    11:10:11 $endpart: 11:10:13 and Runduration: 2 seconds ======================= starttime, $^T without POSIX: 1233763810 Start time, , $^T with POSIX: 11:10:10 2nd POSIX, $^T: 11:10:10 3rd POSIX, current time: 11:10:13 local_time(): Wed Feb 4 11:10:13 2009 POSIX localtime: Wed Feb 11:10:13 2009 ...and after sleeping another second, POSIX localtime: Wed Feb 11:10: +14 2009

    For more precise timeing, read perldoc -q profile.

    See also Time-HiRes and for a *nix-ish flavor, perldoc -q time in (perlfaq8.pod) at "How can I measure time under a second?"

    Update: ...or see oshalla's, above.

Re: Script execution time
by awohld (Hermit) on Aug 05, 2016 at 06:53 UTC
    use Timer::Runtime

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-03-29 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found