I was wondering - given a java code, how can I see how the Java compiler optimizes it?
Are there any JDK tools for this?
Tried to google, but nothing relevant came up.
I was wondering - given a java code, how can I see how the Java compiler optimizes it?
Are there any JDK tools for this?
Tried to google, but nothing relevant came up.
To see what the compiler optimized, simply decompile the bytecode, for instance with javap or JD.
To see what the just in time compiler optimized, activate debug logging (and read the disassembled machine code).
You could use javap
to disassemble the code.
It is worth bearing in mind that javac
generally does very little by way of optimization; in modern JVMs most optimizations are done by the just-in-time compiler that translated java bytecodes to native machine code.
javac
will only do a very little optimization, if any.
Optimization in Java is mostly done by the JIT compiler at runtime. the more optimization JIT performs, the better the code it will generate, but the initial delay will also increase.
To get native code generated by the Java just-in-time compiler: PrintAssembly require OpenJDK 7
Debugging Options: Java HotSpot VM
-XX:-CITime ---- Prints time spent in JIT Compiler. (Introduced in 1.4.0.)
There are tools to convert byte code back to java code. You can compile your code, and take the .class
files, reverse-engineer them to java code and see the optimizations.