Java – no JIT optimization
Look at this question:
code:
class test { public static void main(String abc[]) { for( int k=1; k<=3; k++) { for( int N=1; N<=1_000_000_000; N=N*10) { long t1 = System.nanoTime(); int j=1; for(int i=0; i<=N; i++) j=j*i; long t2 = System.nanoTime() - t1; System.out.println("Time taken for "+ N + " : "+ t2); } } } }
Output of the above code:
Time taken for 1 : 2160 Time taken for 10 : 1142 Time taken for 100 : 2651 Time taken for 1000 : 19453 Time taken for 10000 : 407754 Time taken for 100000 : 4648124 Time taken for 1000000 : 12859417 Time taken for 10000000 : 13706643 Time taken for 100000000 : 136928177 Time taken for 1000000000 : 1368847843 Time taken for 1 : 264 Time taken for 10 : 233 Time taken for 100 : 332 Time taken for 1000 : 1562 Time taken for 10000 : 17341 Time taken for 100000 : 136869 Time taken for 1000000 : 1366934 Time taken for 10000000 : 13689017 Time taken for 100000000 : 136887869 Time taken for 1000000000 : 1368178175 Time taken for 1 : 231 Time taken for 10 : 242 Time taken for 100 : 328 Time taken for 1000 : 1551 Time taken for 10000 : 13854 Time taken for 100000 : 136850 Time taken for 1000000 : 1366919 Time taken for 10000000 : 13692465 Time taken for 100000000 : 136833634 Time taken for 1000000000 : 1368862705
In the loop, even if the value of I starts from 0, indicating that the product is zero, there is no JIT optimization Why not? In the link provided above, I put the for loop in the method call that JIT is optimizing Are statements placed in methods that facilitate the optimization process?
Solution
>In the previous problem, JIT optimizes the complete code at the beginning of the method without analyzing the number in the variable when the method returns This is because you choose to invalidate your method. Give JIT a simple clue, and any calculated value will be discarded. > Comparing the current example with the example in the previous question, the void method is not called, so it will not be optimized Why no other optimization can help this completely different case is an unanswerable question There is no such optimization in the specific JVM implementation and specific JVM calls, and you have used it to test the code