title: Why is Queue.poll faster than Iteration? (java.util.concurrent.ConcurrentLinkedQueue)
body: <p>I have a piece of code that gets all the elements from a queue.  I do not care about the state of the queue afterwards and I can be assured the queue will not be modified while I am removing the elements from it.</p>
  
  <p>I was initially using an iterator to pull the elements because I thought it would be faster than polling the elements...</p>
  
  <p>but i ran the following test:</p>
  
  <pre><code>ConcurrentLinkedQueue&lt;Object&gt; queue = new ConcurrentLinkedQueue&lt;&gt;();
  for (int i=0; i &lt; 1000000; i++)
      queue.add(new Object());
  
  LinkedList&lt;Object&gt; list = new LinkedList&lt;&gt;();
  long start = System.currentTimeMillis();
  for (Object object: queue)
      list.add(object);
  long time1 = System.currentTimeMillis() - start;
  
  list = new LinkedList&lt;&gt;(); 
  start = System.currentTimeMillis();
  Object object;
  while ((object = queue.poll()) != null)
      list.add(object);
  long time2 = System.currentTimeMillis() - start;
  
  System.out.println(time1 + " " + time2);
  </code></pre>
  
  <p>I got the following output (average over 100 runs)</p>
  
  <pre><code>1169 46
  </code></pre>
  
  <p>My question is: Why is poll faster than iterate?  It is completely unintuitive to me because poll will have to modify the queue and iterate will only have to look at the state.</p>
  
  <p>edit --- Gray was right</p>
  
  <p>I ran it in a loop and got the output  (should have done this in the first place)</p>
  
  <pre><code>1180 46
  1422 25
  287 32
  14 26
  226 26
  236 25
  12 26
  14 25
  13 25
  13 26
  13 25
  268 25
  13 25
  14 176
  13 26
  13 26
  13 25
  13 25
  13 26
  13 24
  13 26
  13 25
  ...
  </code></pre>