matija has asked for the wisdom of the Perl Monks concerning the following question:
I need to follow this stuff for various values, and for various periods of time, so I created the following code:
And later:sub average_over_time { # # $ages[x,0] = time of article arrival # $ages[x,1] = age of article; # articles are thrown out of the pipe if they've arrived more than $ma +xage # ago. # my (@ages,$sumages); my $maxage=shift @_; return sub { my $limit=time-$maxage; # Squeeze out events that have timed out while (($#ages>-1) && ($ages[0][0]<$limit)) { $sumages-=$ages[0][1]; shift @ages; } if (defined($_[0])) { push(@ages,[time,$_[0]]); $sumages+=$_[0]; } $sumages=0 if $#ages == -1; return ($#ages>-1)?int($sumages/($#ages+1)):0; } } <code> That gives me the ability to say:<code> my $mail5=average_over_time(300); my $mail60=average_over_time(3600);
I can also get out the average without putting in a value by calling it with $average=&$mail60(undef);.$db{"time-mail5"}=&$mail5($time); $db{"time-mail60"}=&$mail60($time);
Unfortunately, if you have a lot of events during the time period over which you're calculating, the array can get... a bit too large.
So I'm wondering, is there a method that I'm missing? Some way to calculate a running average over a given time period without having to keep all the values of that period in memory? And if at all possible without eating all of the CPU needed by the processes I'm monitoring?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Average over time
by kvale (Monsignor) on Mar 24, 2004 at 17:09 UTC | |
|
Re: Average over time
by hawtin (Prior) on Mar 25, 2004 at 01:51 UTC | |
|
Re: Average over time
by Abigail-II (Bishop) on Mar 24, 2004 at 16:05 UTC | |
by matija (Priest) on Mar 24, 2004 at 16:20 UTC | |
by Roy Johnson (Monsignor) on Mar 24, 2004 at 19:20 UTC |