Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a bad benchmark IMO.</p> <ul> <li>need to repeat in loop multiple times to warm up jvm</li> <li>need to DO something in your iterative loop or it can be optimized array</li> <li><code>ArrayList</code> resizes, which is costly. If you had constructed <code>ArrayList</code> as <code>new ArrayList(500000)</code> you would construct in one blow, and then all allocations would be quite cheap (one preallocating backed array)</li> <li>You don't specify your memory JVM - it should be run with -xMs == -Xmx (everything preallocated) and sufficiently high that no GC is likely to be triggered</li> <li>This benchmark doesn't cover the most unpleasant aspect of LinkedList - random access. (an iterator isn't necessarily the same thing). If you feed say 10% of the size of a large collection as a random selection of <code>list.get</code> you will find linkedlists are awful for grabbing anything other than the first or last element. </li> </ul> <p>For an arraylist: the jdk get is what you'd expect:</p> <pre><code>public E get(int index) { RangeCheck(index); return elementData[index]; } </code></pre> <p>(basically just return the indexed array element.,</p> <p>For a linkedlist:</p> <pre><code>public E get(int index) { return entry(index).element; } </code></pre> <p>looks similar? Not quite. entry is a method not an primitive array, and look what it has to do:</p> <pre><code>private Entry&lt;E&gt; entry(int index) { if (index &lt; 0 || index &gt;= size) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size); Entry&lt;E&gt; e = header; if (index &lt; (size &gt;&gt; 1)) { for (int i = 0; i &lt;= index; i++) e = e.next; } else { for (int i = size; i &gt; index; i--) e = e.previous; } return e; } </code></pre> <p>That's right, if you ask for say <code>list.get(250000)</code>, it's gotta start at the head and repeatedly iterate through the next element. 250000 accesses or so (there's an optimization in the code where it starts at head or tail depending on which would be less accesses.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload