I made this module for my use, It gives me a very simple interface to set timers, multiples... by name. and i can get a "report" that tells me an average of each (if named the same).
I use it like this:

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();

so.. it would output how many runs there were of "one_this" and how long they took on average, etc.

It prints to STDERR, so it's useful for webstuffs, you just read /var/log/apache2/error-log or what have you, in that case

Requires Time::HiRes

package Timers; #BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/srv/www/htdocs/timer +s-log") or die("Unable to open error-log: $!\n"); carpout(LOG);} # op +tional 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:$timeto +tal\n\n"; } #clear history %HISTORY=(); } 1;

It's not a full blown module! It's just a script that solved my needs. Maybe it helps your needs.


In reply to Re: Loop Timing by leocharre
in thread Loop Timing by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.