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; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Acceleration ETA algorithm
by Anonymous Monk on Jan 28, 2024 at 10:28 UTC | |
Re: Acceleration ETA algorithm
by stevieb (Canon) on Jan 27, 2024 at 18:25 UTC | |
by phizel (Acolyte) on Jan 27, 2024 at 19:59 UTC | |
by LanX (Saint) on Jan 28, 2024 at 03:19 UTC | |
by LanX (Saint) on Jan 28, 2024 at 01:13 UTC | |
Re: Acceleration ETA algorithm
by LanX (Saint) on Jan 28, 2024 at 03:38 UTC |