Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks
Can anyone tell me if there is a way to timing and print the time of each interaction inside of a loop?
Like inside of this for loop:
my $i=0; for (my $count=1; $count<11; $count++) { $i++; print "At this time $i - Interaction at this $time and $date<br>"; }

Is that possible?
Thanks

Replies are listed 'Best First'.
Re: Loop Timing
by BMaximus (Chaplain) on Feb 27, 2006 at 17:34 UTC
    This is what Benchmark is for. So if you want to time how long it took for that particular itteration of the loop through then you would do something like this.
    use POSIX; use Benchmark; for (my $count = 0; $count <= 11; $count++) { my $bench_1 = Benchmark->new(); ... # do some intense stuff here my $bench_2 = Benchmark->new(); my $time_diff = timediff($bench_2, $bench_1); print "This interaction took: " . timestr($time_diff) . " on " . strftime("%a %b %e %H:%M:%S %Y", localtime); }


    Update: Woops, flipped $bench_1 and $bench_2 to give the correct timing.

    BMaximus
Re: Loop Timing
by davido (Cardinal) on Feb 27, 2006 at 17:03 UTC

    Do you just want something like this?:

    foreach my $i ( 1 .. 11 ) { print "On iteration $i the time is ", scalar localtime(); }

    For more info see localtime.


    Dave

      And/or Time::HiRes given a sufficiently fast machine (one hopes that it takes less than a second to execute $i++ . . . :).

Re: Loop Timing
by leocharre (Priest) on Feb 27, 2006 at 19:02 UTC

    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.