I have the following code
public class BenchMark { public static void main(String args[]) { doLinear(); doLinear(); doLinear(); doLinear(); } private static void doParallel() { IntStream range = IntStream.range(1, 6).parallel(); long startTime = System.nanoTime(); int reduce = range .reduce((a, item) -> a * item).getAsInt(); long endTime = System.nanoTime(); System.out.println("parallel: " +reduce + " -- Time: " + (endTime - startTime)); } private static void doLinear() { IntStream range = IntStream.range(1, 6); long startTime = System.nanoTime(); int reduce = range .reduce((a, item) -> a * item).getAsInt(); long endTime = System.nanoTime(); System.out.println("linear: " +reduce + " -- Time: " + (endTime - startTime)); } }
I tried to compare threads, but after that the runtime was constantly decreasing, calling the same function again and again
Exit:
linear: 120 -- Time: 57008226 linear: 120 -- Time: 23202 linear: 120 -- Time: 17192 linear: 120 -- Time: 17802 Process finished with exit code 0
There is a huge huge difference between the first and second runtimes .
I'm sure the JVM can do some tricks backstage, but can someone help me understand what is really going on there?
Anyway, to avoid this optimization so that I can check the true runtime?
performance java-8 jvm java-stream
shiva
source share