java - How to determine how the compiler optimizes the code? -
in "java performance definite guide" there's example showing problems writing microbenchmark. code this:
public void test(){ double l; long = system.currenttimemillis(); (int = 0; < n; i++) { l = fib(50); } long = system.currenttimemillis(); system.out.println("time: " + (now - then)); } the book mentions "a smart compiler end executing code" :
long = system.currenttimemillis(); long = system.currenttimemillis(); system.out.println("elapsed time: " + (now - then)); i see "l" local unused variable, , make sense ignore , don't see how it's done or how prove works this. tried running code , seems fib() method executed. looked @ .class file , lines included. how check works mentioned above ?
you mixing java source code bytecode compiler (like javac) , hotspot’s just-in-time compiler. javac doesn’t perform optimizations, therefore won’t see optimizations in .class files.
it’s hotspot engine, or jvm, perform optimizations based on analysis of code , program’s runtime behavior. can’t see these effects (besides performance difference), because can detect difference, placing print statement (or side effect) or setting breakpoint inside method, disable optimization, as, of course, optimizations must not change semantic of running program.
however, result of detection method may still disguise. when place statement system.out.println("hello"); inside method, string "hello" printed accordingly, seeing hello message doesn’t prove numeric calculation did happen, if result still unused.
Comments
Post a Comment