Hmm, rolling averages. You can convert that from N**2 to linear, if you can assume you're moving through the numbers in sequence. Also, converting your hash to an array would be better. Try something like this:
#!/usr/bin/perl use strict; use warnings; ## setup my ($gap, $elements, $first, $i) = (10000, 20000, 10000, 0); my @data = map { rand } (1 .. $elements); ## compute initial sum my $sum = 0; $sum += $_ for @data[0 .. $gap]; ## go through @data serially computing averages while (1) { printf("average of %d to %d: %d", $i + $first, $i + $first + $gap, $sum / $gap); $sum -= $data[$i]; # subtract old 1st element last if ++$i + $gap > $elements; # stop when done $sum += $data[$i + $gap]; # add new last element }
HTH

In reply to Re: calculate average from range of hash values (optimize) by cleverett
in thread calculate average from range of hash values (optimize) by revdiablo

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.