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?
In reply to Average over time by matija
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |