Was looking for a decent algorithm for determining the ETA of a long-running process, but everything on CPAN uses simplistic and inaccurate algorithms. Found this great article

Benchmarking I/O ETA algorithms and converted the Acceleration algorithm to perl. And yes, it would be better to extract the state components into an object.

`use Time::HiRes qw(time);
sub eta {
my ($cur, $total, $time) = @_;
return unless $cur and $time;
state ($last_progress, $last_time, $last_v, $last_eta);
state (@v, @eta, $window_size, $window_idx);
state $init = do {
($last_progress, $last_time, $last_v, $last_eta) = (0, 0, 0, -
+1);
($window_size, $window_idx) = (10, 0);
};
state $sub_v_weight = sub { 1 + $_[0] };
state $sub_eta_weight = sub { $_[0] ? 2 * $_[1] : 1 };
state $sub_weighted_avg = sub {
my ($sub_weight, $avg, $total_weight, $w) = (shift, 0, 0, 0);
for my $i (0 .. $#_) {
# first version messed up the index.
my $j = ($i + @_ - $window_idx - 1) % @_;
$w = $sub_weight->($j, $w);
$avg += $w * $_[$i];
$total_weight += $w;
}
return $avg / $total_weight;
};
my $v = ($cur - $last_progress) / (($time - $last_time) || 1);
$v[$window_idx] = $v;
$v = $sub_weighted_avg->($sub_v_weight, @v);
if ($v and $last_v) {
my ($min_v, $max_v) = $v < $last_v ? ($v, $last_v) : ($last_v,
+ $v);
$v = $last_v + ($v - $last_v) * $min_v / $max_v;
}
my $a = ($v - $last_v) / ($last_time ? ($time - $last_time) : 1);
my $r = $total - $cur;
my $eta = $last_eta;
if ($a and 0 < (my $d = ($v * $v + 2 * $a * $r))) {
$eta = (sqrt($d) - $v) / $a;
}
elsif ($v) { $eta = $r / $v }
$eta[$window_idx] = $eta;
$eta = $sub_weighted_avg->($sub_eta_weight, @eta);
($last_progress, $last_time, $last_v, $last_eta, $window_idx)
= ($cur, $time, $v, $eta, ($window_idx + 1) % $window_size);
return $eta > 0 ? $eta : 0;
}
`

Comment onAcceleration ETA algorithmDownloadCode