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; }

In reply to Acceleration ETA algorithm by phizel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.