If sadmanface hadn't worried about speed, he wouldn't have asked that question, I assume.

Since I couldn't give an algorithm that is faster in the case of small numbers, I gave one that is very much faster for big numbers. He can chose whatever is best for his application.

As to the difference of absolute times, I can just say that it scales with number of times the algorithm is run. Again with can just try to credit sadfaceman some intelligence and assume he wouldn't ask his question about this particular item of code if it wasn't of importance. After all, it cost him some time to ask as well.

Yes, it's a good idea to benchmark, but it still don't think that's a reason to recommend an algorithm that scales bad in the general case, unless you state its limits. And I don't think it's bad to recommend an algorithm that scales well in the general case, even it might be a bit slower for small data structures.


In reply to Re^4: find closest element of array without going over by moritz
in thread find closest element of array without going over by sadfaceman

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.