title: Java ArrayList vs C++ std::vector
body: <p>I was just trying to compare the speed of filtering a List between Java and C++. Just for fun, because I've seen that C++ has std::vector.erase() which removes elements of a vector in place, so I was expecting it to be a lot faster than Java's equivalent, here's the code in Java:</p>
  
  <pre><code>public static void main(String[] args) {
  
      List&lt;Integer&gt; ints = new ArrayList&lt;&gt;(100000000);
      long t1, t2;
      int i;
      t1 = System.currentTimeMillis();
      for (i = 0; i &lt; 100000000; i++) {
          ints.add(i);
      }
      t2 = System.currentTimeMillis();
  
      System.out.println("Initial array generated in " + (t2 - t1) + "ms");
  
      t1 = System.currentTimeMillis();
      List&lt;Integer&gt; result = ints.stream().filter((e) -&gt; e % 2 == 0).collect(Collectors.toList());
      t2 = System.currentTimeMillis();
  
  
      System.out.println("Result: " + result.size() + " in " + (t2 - t1) + "ms.");
  }
  </code></pre>
  
  <p>And the output:</p>
  
  <pre><code>Initial array generated in 21859ms
  Result: 50000000 in 3135ms.
  </code></pre>
  
  <p>Here's the C++ equivalent:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;sys/time.h&gt;
  
  long gettime(){
  
     struct timeval time;
     gettimeofday(&amp;time, NULL);
  
     long microsec = ((unsigned long long)time.tv_sec * 1000000) + time.tv_usec;
  
     return microsec / 1000;
  
  }
  int main(int argc, char **argv){
  
    std::vector&lt;int&gt; ints;
  
    long t1 = gettime();
    for(int i=0; i&lt; 100000000; i++){
      ints.push_back(i);
    }
    long t2 = gettime();
  
    std::cout &lt;&lt; "Initial array generated in " &lt;&lt; (t2-t1) &lt;&lt; "ms\n";
  
  
    t1 = gettime();
    ints.erase(std::remove_if(begin(ints), end(ints),
                 [](const int i){ return i%2==0;}), end(ints));
  
    t2 = gettime();
    std::cout &lt;&lt; "Result: " &lt;&lt; ints.size() &lt;&lt; " in " &lt;&lt; (t2-t1) &lt;&lt; "ms.\n";
  
   return 0;
  }
  </code></pre>
  
  <p>And the C++ output:</p>
  
  <pre><code>Initial array generated in 1357ms
  Result: 50000000 in 1323ms.
  </code></pre>
  
  <p>Ok, so the array filtering is x3 faster in C++ (I was expecting more, to be honest). The question though is, why is Java so slow at populating the List (22sec)?</p>