i am playing around with java 1.8. I read that the new collection api works faster because it runs the operations on the data structures parallelized.
I want to see this efford. So i wrote following code:
public static void main(String[] args) {
ArrayList<SamplePerson> persons = new ArrayList<>();
for (long i = 0; i < 10000000; i++) {
persons.add(new SamplePerson(EyeColour.BLUE,20,"Max Musterman",Gender.MALE));
}
long nsBefore = System.nanoTime();
// using the new collection api - parallel way??
persons.forEach(samplePerson -> samplePerson.setAge(22));
// sequential way
for(int i = 0; i < persons.size(); i++){
persons.get(i).setAge(22);
}
long nsAfter = System.nanoTime();
long runtime = nsAfter - nsBefore;
System.out.println("Time in Nanoseconds: " + runtime);
}
My Processor: i7-2600 CPU
Result using the "parallel" way: Time in Nanoseconds: 74836825
Result using the sequential way: Time in Nanoseconds: 45071315
Could everybody explain this results. Is the overhead to set up this threats that high? I am a bit confused please help me :-)