Devel::Timer(3)
πΈ NAME πΈ
okay so this page is Devel::Timer(3) from the User Contributed Perl Documentation, and bestie, it is literally all about tracking and reporting execution time for parts of your code π
β¨ VERSION β¨
version 0.14 β yes, that's the exact version, no cap π
π SYNOPSIS π
use Devel::Timer;
my $t = Devel::Timer->new();
$t->mark('first db query');
## do some work
$t->mark('second db query');
## do some more work
$t->mark('end of second db query');
$t->report();
π¦ DESCRIPTION π¦
Devel::Timer lets developers accurately time how long a specific piece of code takes to execute. This can be helpful in locating the slowest parts of an existing application. Lowkey iconic for profiling, fr fr π₯
First, the Devel::Timer module is used and instantiated.
use Devel::Timer;
my $t = Devel::Timer->new();
Second, markers are placed before and after pieces of code that need to be timed. For this example, we are profiling the methods get_user_score() and get_average_user_score().
$t->mark('first db query');
&get_user_score($user);
$t->mark('second db query');
&get_average_user_score();
Finally, at the end of the code that you want to profile, an end marker is placed, and a report is generated on stderr.
$t->mark('END');
$t->report();
Sample report:
Devel::Timer Report -- Total time: 0.3464 secs
Interval Time Percent
----------------------------------------------
02 -> 03 0.3001 86.63% second db query -> END
01 -> 02 0.0461 13.30% first db query -> second db query
00 -> 01 0.0002 0.07% INIT -> first db query
The report is output using the method Devel::Timer::print() which currently just prints to stderr. If you want to send the output to a custom location you can override the print() method. The initialize() and shutdown() methods can also overridden if you want to open and close log files or database connections π
If you launch your program in a cycle (e.g. it's a web app which processes queries) you might want initialize timer once and then take measurements repeatedly. In that case you can use reset() method. After calling this method all accumulated data by timer will be deleted and timer will start countdown again.
$t->reset();
$t->report();
Sample report:
Devel::Timer Report -- Total time: 0.3462 secs
Interval Time Percent
----------------------------------------------
01 -> 02 0.3001 86.70% second db query -> END
00 -> 01 0.0461 13.30% first db query -> second db query
Pay attention: after callingreset()method there will be no markINIT. This mark is only set once while callingnew()method. π«Ά
π METHODS π
π§ new() π§
Create a new instance. No parameters are processed.
π initialize() π
Empty method. Can be implemented in the subclass.
β mark($name) β
Set a timestamp with a $name.
π reset() π
Delete all data accumulated by timer.
π print() π
Prints to STDERR. Can be overridden in the subclass.
π report(%args) π
Prints the report to STDOUT. The %args can have the following keys:
collapseβ can be0or1sort_byβ can be'time'or'count'
By default report() looks like this:
$t->report;
Devel::Timer Report -- Total time: 7.0028 secs
Interval Time Percent
----------------------------------------------
05 -> 06 3.0006 42.85% something begin -> something end
03 -> 04 2.0007 28.57% something begin -> something end
06 -> 07 1.0009 14.29% something end -> END
01 -> 02 1.0004 14.29% something begin -> something end
00 -> 01 0.0000 0.00% INIT -> something begin
04 -> 05 0.0000 0.00% something end -> something begin
02 -> 03 0.0000 0.00% something end -> something begin
Which is great for small or non-iterative programs, but if there's hundreds of loops of 'something begin -> something end' the report gets very painful very quickly. :)
In that scenario you might find collapse useful:
$t->report(collapse => 1);
Devel::Timer Report -- Total time: 7.0028 secs
Count Time Percent
----------------------------------------------
3 6.0018 85.71% something begin -> something end
1 1.0009 14.29% something end -> END
2 0.0001 0.00% something end -> something begin
1 0.0000 0.00% INIT -> something begin
The stats for all combinations of labels are added together.
We also accept a sort_by parameter. By default the report is sorted by total time spent descending (like the default report()), but you can sort by count descending instead if you want:
$t->report(collapse => 1, sort_by => 'count');
Devel::Timer Report -- Total time: 7.0028 secs
Count Time Percent
----------------------------------------------
3 6.0018 85.71% something begin -> something end
2 0.0001 0.00% something end -> something begin
1 0.0000 0.00% INIT -> something begin
1 1.0009 14.29% something end -> END
π¦ get_stats($start, $end) π¦
Returns the accumulated statistics for a specific a combination of mark()'s that have occurred while your program ran. These values are the exact same statistics that report() prints. get_stats() simply returns them to you so you can do something creative with them β¨
For example, to get the cumulative stats for every time your program has specifically moved from mark('X') to mark('Y'), you can run this:
my ($time, $percent, $count) = $t->get_stats('X', 'Y');
$time is the total number of seconds elapsed between 'X' and 'Y'.
$percent is the percentage of total program run time that you have spent between 'X' and 'Y'.
$count is the number of times your program has moved from 'X' to 'Y'.
π shutdown π
Empty method. Can be implemented in subclass.
π§ SUBCLASSING π§
package MyTimer;
use strict;
use Devel::Timer;
use vars qw(@ISA);
@ISA = ('Devel::Timer');
sub initialize
{
my $log = '/tmp/timer.log';
open(my $LOG, '>>', $log) or die("Unable to open [$log] for writing.");
}
sub print
{
my($self, $msg) = @_;
print $LOG $msg . "\n";
}
sub shutdown
{
close $LOG;
}
You would then use the new module MyTimer exactly as you would use "Devel::Timer".
use MyTimer;
my $t = MyTimer->new();
$t->mark('about to do x');
$t->mark('about to do y');
$t->mark('done y');
$t->report();
π₯ TO DO π₯
"Devel::Timer" does not currently do any reporting or statistics of any kind based on nested trees of mark() calls. So if your program runs these mark() calls:
A
B, C
B, C
D
E
"Devel::Timer" never tells you anything about how much time you spent moving from A to D. Depth aware reporting might be an interesting project to tackle π
πΈ SEE ALSO πΈ
Time::HiRes
π REPOSITORY π
<https://github.com/manwar/Devel-Timer>
π LICENSE AND COPYRIGHT π
Jason Moore
This is free software. It is licensed under the same terms as Perl itself.
π AUTHOR π
- Author: Jason Moore -
jmoore@sober.com(no longer valid) - Maintainer: Gabor Szabo -
gabor@pti.co.il - Contributor: Jay Hannah -
jay@jays.net
Currently maintained by Mohammad S Anwar (MANWAR) - mohammad.anwar@yahoo.com
cute sign-off time: this page is from perl v5.40.0, dated 2024-08-05, and it's Devel::Timer(3) β mwah, go profile something slay πβ¨
π¦ where this page comes from
- epel 10: perl-Devel-Timer 0.14-7.el10_0 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms