Timer::Simple - Small, simple timer (stopwatch) object
use Timer::Simple (); my $t = Timer::Simple->new(); do_something; print "something took: $t\n"; # or take more control my $timer = Timer::Simple->new(start => 0, string => 'human'); do_something_before; $timer->start; do_something_else; print "time so far: ", $t->elapsed, " seconds\n"; do_a_little_more; print "time so far: ", $t->elapsed, " seconds\n"; do_still_more; $timer->stop; do_something_after; printf "whole process lasted %d hours %d minutes %f seconds\n", $t->hms; # or simply "whole process lasted $t\n" with 'string' => 'human' $timer->restart; # use the same object to time something else # you can use package functions to work with mutliple timers $timer1 = Timer::Simple->new; do_stuff; $timer1->stop; do_more; $timer2 = Timer::Simple->new; do_more_stuff; $timer2->stop; print "first process took $timer1, second process took: $timer2\n"; print "in total took: " . Timer::Simple::format_hms($timer1 + $timer2);
This is a simple object to make timing an operation as easy as possible.
It uses Time::HiRes if available (unless you tell it not to).
It stringifies to the elapsed time (see "string").
This module aims to be small and efficient and do what is useful in most cases while also being sufficiently customizable.
Constructor; Takes a hash or hashref of arguments:
hires- Boolean; Defaults to true;
sprintfstring used by "hms"
start- Boolean; Defaults to true;
Set this to false to skip the initial setting of the clock. You must call "start" explicitly if you disable this.
string- The default format for "string". Defaults to
Returns the number of seconds elapsed since the clock was started.
This method is used as the object's value when used in numeric context:
$total_elapsed = $timer1 + $timer2;
# list my @units = $timer->hms; sprintf("%d hours %minutes %f seconds", $timer->hms); # scalar print "took: " . $timer->hms . "\n"; # same as print "took :$timer\n"; # alternate format $string = $timer->hms('%04d h %04d m %020.10f s');
Separates the elapsed time (seconds) into hours, minutes, and seconds.
In list context returns a three-element list (hours, minutes, seconds).
In scalar context returns a string resulting from
sprintf($format, $h, $m, $s)).
The default format is
%02d:%02d:%9.6f) with Time::HiRes or
format can be specified in "new"
or can be passed as an argument to the method.
Initializes the timer to the current system time.
Stop the timer. This records the current system time in case you'd like to do more processing (that you don't want timed) before reporting the elapsed time.
print $timer->string($format); print "took: $timer"; # stringification equivalent to $timer->string()
Returns a string representation of the elapsed time.
The format can be passed as an argument. If no format is provided
the value of
string (passed to "new") will be used.
The format can be the name of another method (which will be called), a subroutine (coderef) which will be called like an object method, or one of the following strings:
short- Total elapsed seconds followed by
rps- Total elapsed seconds followed by requests per second:
human- Separate units spelled out:
'6 hours 4 minutes 12 seconds'
full- Total elapsed seconds plus
'2 seconds (0 hours 0 minutes 2 seconds)'
This is the method called when the object is stringified (using overload).
The following functions should not be necessary in most circumstances but are provided for convenience to facilitate additional functionality.
Indicates whether Time::HiRes is available.
$spec = default_format_spec(); # consults HIRES() $spec_whole = default_format_spec(0); # false forces integer $spec_fractional = default_format_spec(1); # true forces fraction
Returns an appropriate
sprintf format spec according to the provided boolean.
If true, the spec forces fractional seconds (like
If false, the spec forces seconds to an integer (like
If not specified the value of "HIRES" will be used.
my $string = format_hms($hours, $minutes, $seconds); my $string = format_hms($seconds);
Format the provided hours, minutes, and seconds into a string by guessing the best format.
If only seconds are provided the value will be passed through "separate_hms" first.
my ($hours, $minutes, $seconds) = separate_hms($seconds);
Separate seconds into hours, minutes, and seconds. Returns a list.
These are some other timers I found on CPAN and how they differ from this module:
- Time::Elapse - eccentric API to a tied scalar
- Time::Progress - Doesn't support Time::HiRes
- Time::Stopwatch - tied scalar
- Dancer::Timer - inside Dancer framework
You can find documentation for this module with the perldoc command.
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
A modern, open-source CPAN search engine, useful to view POD in HTML format.
Bugs / Feature Requests
Please report any bugs or feature requests by email to
bug-timer-simple at rt.cpan.org, or through
the web interface at https://rt.cpan.org/Public/Bug/Report.html?Queue=Timer-Simple. You will be automatically notified of any
progress on the request by the system.
git clone https://github.com/rwstauner/Timer-Simple.git
Randy Stauner email@example.com
COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Randy Stauner.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.