Another really really nice module I found while looking for datadumper was Devel::AutoProfiler. It basicly examines and profiles sub routine calls.
The use of it is simple, your first line after interpreter intialization is use Devel::AutoProfiler. It will then profile and time all calls your code makes during execution. If you have say 'use strict' and you dont want to profile the strict module, simply place the use statement after the use strict (examples below).
I recently started in on creating a MUD in perl, just cause it's a decent excercise. With the use of Devel::AutoProfiler, I have already sped up my program in a few different places. It isn't a really fine grained tool, but it can be very usefull to get an overall feel for whats going on within your program at a high level, and provide points to attack within your codebase.
Note: requires Time::HiRes
#!/usr/bin/perl
use Devel::AutoProfiler;
&do_it();
sub do_it {
print "Hello world\n";
for ( 1 .. 20 ) {
print "$_\n";
}
}
# OUTPUT BELOW : I have removed code output for legibility
$VAR1 = [
'main::do_it',
{
'total_calls' => '1',
'file -, line 3' => '1',
'total_time_in_sub' => '0.003332'
}
];
Example 2
#!/usr/bin/perl
# We dont want to profile strict, so place the autoprofiler
# line after that module.
use strict;
use Devel::AutoProfiler;
for ( 1 .. 20 ) {
&read_file('/etc/passwd');
}
sub read_file {
my $file = shift();
open(IN, "$file") || die "Cant access $file\nReason: $!\n";
while (<IN>) {
print "$_";
}
close(IN);
return;
}
# Added comments to profiler output and apologies if people
# dont like the overuse of # signs
# OUTPUT BELOW again without actuall code output
#
$VAR1 = [
#
# The subroutine name called
#
'main::read_file',
{
#
# file and line in codebase that routine was called from,
# I did this via STDIN, so file is a -, and the call was on
# line 6
#
'file -, line 6' => '20',
#
# number of times the routine was called
#
'total_calls' => '20',
#
# the total time it spent in this particular sub
#(in seconds)
#
'total_time_in_sub' => '0.090052'
}
];
Granted these are contrived examples off the top of my head, but if you were to have a few sub routines nested, and one is taking an inordinate amount of time to run, at least you can see it.
Benchmark is a great tool, but I sometimes find it hard to take large/complex segments of code and pass it off the same way it would in my program. With AutoProfiler, I simply run the code and can quickly see where my time is taken up.
Hope this helps some, even though it isn't directly related to reading the output from Benchmark.
/* And the Creator, against his better judgement, wrote man.c */
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.