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

Experts,
I have the following output from Benchmark's time diff. The problem I have is that as the wallclock is represented in seconds it is very hard to understand.
Counted 900285 lines It took 6245 wallclock secs ( 6242.69 usr + 1.87 sys = 6244.56 CPU)
The result above is generated with this code:
#!/usr/bin/perl -w use Benchmark; my $start_time = new Benchmark; $line++ while (<>); # Count number of lines in the file(s) my $end_time = new Benchmark; my $difference = timediff($end_time, $start_time); print ("Counted $line lines\n"); print ("It took ", timestr($difference), "\n");
How can I convert the wallclock seconds above into a more understandable output format? Namely from seconds into hours and minute.

Replies are listed 'Best First'.
Re: Simplifying Timediff Output from Seconds to Hours and Minute
by GrandFather (Saint) on Sep 25, 2006 at 03:52 UTC

    The documentation for Benchmark notes:

    The data is stored as a list of values from the time and times functions:

    ($real, $user, $system, $children_user, $children_system, $iters)

    in seconds for the whole loop (not divided by the number of rounds).

    so the first element of the array referenced by a benchmark object is a time in seconds. The following code extracts the time in seconds and converts it to hours, minutes and seconds in a fairly nieve fashion:

    use strict; use warnings; use Benchmark; my $start_time = new Benchmark; sleep (2); # Do stuff my $end_time = new Benchmark; my $difference = timediff($end_time, $start_time); my $seconds = $difference->[0]; my $hours = int ($seconds / 3600); my $mins = int (($seconds %= 3600) / 60); $seconds -= $mins * 60; print ("It took $hours hours, $mins minutes and $seconds seconds.\n");

    DWIM is Perl's answer to Gödel
Re: Simplifying Timediff Output from Seconds to Hours and Minute
by McDarren (Abbot) on Sep 25, 2006 at 04:24 UTC
    For several different approaches to converting seconds into DHMS, you may be interested to take a look at this thread.

    Cheers,
    Darren :)

Re: Simplifying Timediff Output from Seconds to Hours and Minute
by monkfan (Curate) on Sep 25, 2006 at 06:59 UTC
    Time::Convert can be helpful here.
    use Time::Convert; # skip other code my $tmstr = timestr($difference); $tmstr =~ /(\d+) wallclock secs/; my $convert = new Time::Convert; my $actual = $convert->ConvertSecs($1); print( "It took $actual\n" );

    Regards,
    Edward
Re: Simplifying Timediff Output from Seconds to Hours and Minute
by Limbic~Region (Chancellor) on Sep 25, 2006 at 12:52 UTC
    Anonymous Monk,
    There are a number of canned ways of doing this as noted elsewhere in the thread. One of my first attempts at writing a module was reinventing this wheel. Perhaps you might find something in it useful or interesting.

    Cheers - L~R