"IK" == Ilmari Karonen <[EMAIL PROTECTED]> writes:
IK> Isn't this rather needlessly complex?
Nope, not needlessly.
IK> Essentially the same results can be achieved by simply counting
IK> the (possibly inaccurate) time taken by sleep() as part of the
IK> next iteration. Like this:
use Time::HiRes qw(time sleep); # optional, for subsecond precision
my $step = 0.1; # ten iterations per second
my $t = time;
while (1) {
# do stuff...
} continue {
$t += $step; # watch out for rounding errors!
my $delay = time - $t;
sleep $delay if $delay > 0;
}
I think you meant $t - time;
IK> This is guaranteed to maintain the correct average rate regardless
IK> of any variations in the execution time of the loop, and will
IK> automatically compensate for any systematic errors in sleep().
IK> Non-systematic errors, of course, are unavoidable, but are
IK> guaranteed not to propagate.
Ah, but I don't want to maintain the average rate in all cases. This
code was written to limit packets being sent out over the network. If
the packet sending code falls behind significantly for some reason
(say because someone paused the process), I don't want the code to run
through the loop as quickly as possible till it brings the average
rate back up. Rather, the code gives up if it has fallen too far
behind and resets the rate (I'll make how far behind it can fall
configurable).
j.