0

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 :-)

jmj
  • 237,923
  • 42
  • 401
  • 438
Michael
  • 19
  • 2
  • Please show your sequential code. – Artjom B. Jun 19 '14 at 17:46
  • 5
    possible duplicate of [How do I write a correct micro-benchmark in Java?](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java). I mean: Benchmarking in Java is hard and naive approaches are deemed to fail. – maaartinus Jun 19 '14 at 18:09
  • By the way, the default implementation of `forEach()` (as well as the `ArrayList` implementation) is just a regular for-each loop, so (as pointed out below) it probably isn't parallel. – awksp Jun 19 '14 at 18:16
  • at Jigar Joshi: thank you for that quick respons. That seems to be the solution. Now i used the parallel Stream and initiated a warmup phase (i ran a bunch of loops before measuring). Results: Result using the parallel way: Time in Nanoseconds: 41721340. Result using the sequential way: Time in Nanoseconds: 43841863 at maaartinus: I noticed that benchmarking in java isn't as easy as i thought.So i guess that this measuring isn't very representiv at all. – Michael Jun 19 '14 at 20:02

1 Answers1

6

you need to consider JIT warmup while benchmarking something, I would let it run first time and in second iteration benchmark the same thing in this case

also you are not using parallelStream you need

persons.parallelStream().forEach()
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 4
    Even the "run it a few times" advice here is too naive to give you real performance numbers. You need to use a real performance framework like JMH for measurement. – Brian Goetz Jun 19 '14 at 19:03