For flat arrays, in order to insert an element in an array of N elements, you have to perform two operations: 1) find the insertion point which is an O(N) operation, and 2) insert the new value there which is also O(N) because you have to move all the elements after the insertion point by one place. Globally the operation is O(N).

With lists, step 2, is cheaper, but you still have to find the insertion point in the list which is O(N), so globally, the operation remains O(N).

Lists are cache unfriendly by two reasons: 1) they use more memory than arrays (for built-in types at least x2, but x4 or x8 commonly) and 2) they may be scattered in memory and turn the cache prefetching useless. So it is easy to get into a situation where advancing to the next element requires always going to L3 or even RAM.

In contrast, navigating an array, even when it doesn't fit into L2, is much faster because the cache prefetching is fully effective.

BTW, You get O(logN) insertions when you use a tree.


In reply to Re^7: [OT] The interesting problem of comparing (long) bit-strings. by salva
in thread [OT] The interesting problem of comparing bit-strings. by BrowserUk

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.