use lib '/where/you/have/Timers.pm'; use Timers.pm Tstart('ok'); for (@these){ Tstart('one_this'); do { this($_);}; Tstop('one_this'); } for (@these2){ Tstart('one_this2'); do { this($_);}; Tstop('one_this2'); } Tstop('ok); Treport(); #### package Timers; #BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/srv/www/htdocs/timers-log") or die("Unable to open error-log: $!\n"); carpout(LOG);} # optional other log use vars qw{$VERSION @ISA @EXPORT @EXPORT_OK}; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(&Tstop &Tstart &Treport); # just to calling package $VERSION = '0.01'; use strict; use Time::HiRes qw(gettimeofday); my %CONF =( human_legible=>1, # if on, uses limit for shown second vals decimals=>3 # default decimal places for human legible vals ); # running timers our %TIMERS=(); # stored results, arrays labeled after timer labels our %HISTORY=(); sub Tstart { my $label=$_[0]; $TIMERS{$label}=gettimeofday; } sub Tstop { no warnings; my $label=$_[0]; my $elapsed = (gettimeofday-$TIMERS{$label}); delete $TIMERS{$label}; push @{$HISTORY{$label}}, $elapsed; if ($CONF{human_legible}){ $elapsed = sprintf("%.$CONF{decimals}f",$elapsed); } return $elapsed;#someone may want to use that right away. } sub Treport { print STDERR "--\nTimers.pm Report ..\n".`date`."\n"; for ( sort { $HISTORY{$a} cmp $HISTORY{$b} } keys %HISTORY ){ my $timer=$_; my $total=scalar(@{$HISTORY{$timer}}); my $timetotal=0; for (@{$HISTORY{$timer}}){ $timetotal = ( $timetotal + $_ ); } my $average = ( $timetotal / $total ); if ($CONF{human_legible}){ $timetotal = sprintf("%.$CONF{decimals}f",$timetotal); $total = sprintf("%.$CONF{decimals}f",$total); $average = sprintf("%.$CONF{decimals}f",$average); } print STDERR "$timer\n [$total] avg: $average\n sum:$timetotal\n\n"; } #clear history %HISTORY=(); } 1;