Time::Stopwatch3pm
Use tied scalars as timers
Usage
use Time::Stopwatch;tie my $timer, 'Time::Stopwatch';do_something();print "Did something in $timer seconds.\n";my @times = map {$timer = 0;do_something_else();$timer;} 1 .. 5;
Overview
The Time::Stopwatch module provides a convenient interface to timing functions through tied scalars. From the point of view of the user, scalars tied to the module simply increase their value by one every second.
Using the module should mostly be obvious from the synopsis. You can provide an initial value for the timers either by assigning to them or by passing the value as a third argument to tie().
If you have the module Time::HiRes installed, the timers created by Time::Stopwatch will automatically count fractional seconds. Do not assume that the values of the timers are always integers. You may test the constant "Time::Stopwatch::HIRES" to find out whether high resolution timing is enabled.
A note on timing short intervals
Time::Stopwatch is primarily designed for timing moderately long intervals (i.e. several seconds), where the overhead imposed by the tie() interface does not matter. With Time::HiRes installed, it can nonetheless be used for even microsecond timing, provided that appropriate care is taken.
- •
-
Explicitly initialize the timer by assignment. The first measurement taken before resetting the timer will be a few microseconds longer due to the overhead of the
tie()call.
- •
-
Alwayssubtract the overhead of the timing code. This is true in general even if you're not using Time::Stopwatch. (High-level benchmarking tools like Benchmark.pm do this automatically.) See the code example below.
- •
-
Take as many measurements as you can to minimize random errors. The Statistics::Descriptive module may be useful for analyzing the data. This advice is also true for all benchmarking.
- •
-
Remember that a benchmark measures the time take to run the benchmark. Any generalizations to real applications may or may not be valid. If you want real world data, profile the real code in real use.
The following sample code should give a relatively reasonable measurement of a the time taken by a short operation:
use Time::HiRes; # high resolution timing requireduse Time::Stopwatch;use Statistics::Descriptive;my $stat = Statistics::Descriptive::Sparse->new();tie my $time, 'Time::Stopwatch'; # code timertie my $wait, 'Time::Stopwatch'; # loop timerwhile ($wait < 60) { # run for one minutemy $diff = 0;$time = 0; do_whatever(); $diff += $time;$time = 0; $diff -= $time;$stat->add_data($diff);}print("count: ", $stat->count(), " iterations\n","mean: ", $stat->mean(), " seconds\n","s.d.: ", $stat->standard_deviation(), " seconds\n");
Note that the above code includes the time of the subroutine call in the measurement.
Change log
- 1.00 (15 Mar 2001)
-
Explicitly localized
$SIG{__DIE__}when testing for Time::HiRes availability. Added "A note on timing short intervals" to the POD documentation. Bumped version to 1, no longer beta.
- 0.03 (27 Feb 2001)
-
Modified tests to give more information, reduced subsecond accuracy test to 1/10 seconds to allow for inaccurate
select()implementations. Tweaked synopsis and README.
Releases
10 releases since Debian 4.0 (Etch); the text changed 5 times.