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:
public static void main(String[] args) {
List<Integer> ints = new ArrayList<>(100000000);
long t1, t2;
int i;
t1 = System.currentTimeMillis();
for (i = 0; i < 100000000; i++) {
ints.add(i);
}
t2 = System.currentTimeMillis();
System.out.println("Initial array generated in " + (t2 - t1) + "ms");
t1 = System.currentTimeMillis();
List<Integer> result = ints.stream().filter((e) -> e % 2 == 0).collect(Collectors.toList());
t2 = System.currentTimeMillis();
System.out.println("Result: " + result.size() + " in " + (t2 - t1) + "ms.");
}
And the output:
Initial array generated in 21859ms
Result: 50000000 in 3135ms.
Here's the C++ equivalent:
#include <iostream>
#include <vector>
#include <algorithm>
#include <sys/time.h>
long gettime(){
struct timeval time;
gettimeofday(&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<int> ints;
long t1 = gettime();
for(int i=0; i< 100000000; i++){
ints.push_back(i);
}
long t2 = gettime();
std::cout << "Initial array generated in " << (t2-t1) << "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 << "Result: " << ints.size() << " in " << (t2-t1) << "ms.\n";
return 0;
}
And the C++ output:
Initial array generated in 1357ms
Result: 50000000 in 1323ms.
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)?