How long did something take in Perl?

Some things in Perl seem very obvious to the gurus but aren't terribly obvious to us mere mortals. I'm writing scripts for work to automagically publish our docs, so I'm trying to work out how long each document takes to publish.

This uses the Time::Duration module to convert the epoch seconds that Perl uses into something humans can read. It's adaptive to give something meaninful, so "66666666" returns "2 years and 42 days" while "666" returns "11 minutes and 6 seconds". If you want full precision, there is also the duration_exact function in that module.

use Time::Duration qw(duration);

# Record the start time, in epoch seconds
$starttime = time;

# Do stuff
...

# Record the end time, in epoch seconds
$endtime = time;

# Subtract the start time from the end time
$difference = $endtime - $starttime;

# Convert the seconds to something humans can read
$duration = duration($difference);

print "The process took: $duration\n";
0 responses