If I understand you correctly, your ranges are all essentially independent of each other.

One thing I would definitely suggest is buckets for the ranges. Have an array of 2k buckets, with each bucket covering 2000 values. For each range, push a reference to it into the buckets it touches. That way, you only have to inspect a couple of ranges (avg of 30-40 I expect) for each value, and it should speed up the calculation, at the cost of a little pre-processing work.

Addendum:

On further thought, this would be quite different and possibly slower than your current scheme, which is pretty good.

For a refinement, what if you simplify the calculation of distance to the edge point? Try breaking up the for loop over the range into two loops. The first goes from zero distance from the start point up to max at halfway through the range. The second goes from max at halfway, down to zero at the end point.

Then you can simply say $score = max($score, $loopIndex);


In reply to Re: Can I speed this up? by SuicideJunkie
in thread Can I speed this up? (repetitively scanning ranges in a large array) by daverave

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.