title: Java - Vector vs ArrayList performance - test
body: <p>Everybody's saying that one should use vector because of the perfomance (cause Vector synchronizes after every operation and stuff). I've written a simple test:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Date;
  import java.util.Vector;
  
  public class ComparePerformance {
  
      public static void main(String[] args) {
          ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
          Vector&lt;Integer&gt; vector = new Vector&lt;Integer&gt;();
  
          int size = 10000000;
          int listSum = 0;
          int vectorSum = 0;
  
          long startList = new Date().getTime();
          for (int i = 0; i &lt; size; i++) {
              list.add(new Integer(1));
          }
          for (Integer integer : list) {
              listSum += integer;
          }
          long endList = new Date().getTime();
          System.out.println("List time: " + (endList - startList));
  
          long startVector = new Date().getTime();
          for (int i = 0; i &lt; size; i++) {
              vector.add(new Integer(1));
          }
          for (Integer integer : list) {
              vectorSum += integer;
          }
          long endVector = new Date().getTime();
          System.out.println("Vector time: " + (endVector - startVector));
      }
  }
  </code></pre>
  
  <p>The results are as follows:</p>
  
  <pre><code>List time: 4360
  Vector time: 4103
  </code></pre>
  
  <p>Based on this it seems that <code>Vector</code> perfomance at iterating over and reading is slightly better. Maybe this is a dumb queston or I've made wrong assumptions - can somebody please explan this?</p>