I’m studying the ways to improve performance of Java code. I’ve written some kind of benchmark which executes 100_000_000 times 2 functions and measure the time of their execution.
The first function uses Java Stream API, the second one uses regular for-loop, if statements and so on. I believe it’s obvious that the first function should take more time than the second one because Stream API is the way “heavier” than regular procedures. But the execution time of these functions are almost the same.
The main problem here is JIT which is kind of black box that, I think, knows how to cut off extra code or replace objects with static functions or just inline some values without its calculation if they’re calculated many time.
So my main problem is to make up with such java code which would:
- hard for JIT to optimize
- easy for processor to execute. Otherwise it would take so much processor time and I won’t see if my optimizations are actually speeding up something
Is there a universal algorithm which satisfies these conditions?