0

I have a Java application and one of the methods is performance-critical.

I created a loop to call this method 10 times and I am checking for performance issues by using the profiler for every iteration. It turned out that the execution time decreases by iterations. Thus, the 10th iteration has a smaller execution time than then 9th iteration.

Any idea why such case is happening? Could it be due to the loop overheads?

kidra.pazzo
  • 195
  • 8

1 Answers1

1

You are warming the CPU caches, and the JVM thus the performance changes.

Profillers put the JVM into an unusual mode, and depending on what profiler approach you are using then it may only be sampling at a regular interval.

I find that profillers are good for giving you relative measurements and to improve your understanding of the code; but always take their reading with a pinch of salt.

Do not trust just a single measurement.

Outside of using profillers, microbenchmarking is a good way to go. Although it is a very tricky subject.

Note that Hotspot tends not to kick in and optimise the byte codes until the target code has been called 10,000 or more times.

http://java.dzone.com/articles/microbenchmarking-java, and How do I write a correct micro-benchmark in Java? may help to get you started. There is also a lot of good advice on the Mechanical Sympathy Forum.

A good microbenchmarking framework is here http://openjdk.java.net/projects/code-tools/jmh/, it helps keep GC, and other JVM stop-the-world events out of the timings. As well as some guidence on how to prevent Hotspot from optimising out the very code that you are trying to measure.

Community
  • 1
  • 1
Chris K
  • 11,622
  • 1
  • 36
  • 49
  • Just Adding to this answer: The part of Hotspot JVM that does the optimizations is called JIT (Just In Time Compiler) and it kicks in depending on which one you are using, as of now two options -server and -client, -server will kick in at 15000 executions and perform aggresive optimizations upon the code, and -client will kick in at 1500 I believe, check your specific JVM docs to make sure. – bubooal Jul 03 '14 at 17:00
  • And think of profiling output data as orders of magnitude, and always aim your tuning efforts on the highest one. As another addition, there are performance tests, system tests, micro benchmark and macro benchmark which could help you, google them and decide which approach would be best for your case, micro and macro benchmark are really tricky, I recoomend you become really familiar with the JVM before attempting them, you need to know all that is in play and how to truly get representative numbers watching factors like JIT optimizations, GC Latencies etc... – bubooal Jul 03 '14 at 17:02