Please try running your code with my simple 3 ranges example (and max_length = 10). I think you get different results, but I only had a quick look at your code and perhaps it's related to the fact your coordinates start from 0, not 1 as I mentioned?

I tried shifting all my ranges by -1 and run your code again, but I still different results:

my @universe = (0, 9); my @ranges = ( [ 0, 2 ], [ 1, 6 ], [ 9, 1 ] ); FINAL: 3 5 5 7 7 5 3 3 3 3
Specifically, note that there are coordinates (8 and 9 or 7 and 8 in the shifted version) that are not covered by any range, thus you must have ones in your results.

Update: I forgot to write I really appreciate you spending time in your lunch break (or any other break...) helping me. Thank you!

Update following your corrected code: I ran your code against my original version on some relatively small example (max_length = 1M, some 6k ranges of a few k's each). My original version was done in about 3 minutes.

The good news is the results are the same. The bad news is your version ran for more than 18 minutes. Perhaps all the grepping is expensive? (also remember this array is relatively small, only 6k ranges. I usually have about 25k ranges, which means the grepped arrays will be 4 times larger).


In reply to Re^3: Can I speed this up? by daverave
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.