2

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.

sara
  • 3,824
  • 9
  • 43
  • 71
  • 1
    Java compilers don't do much optimization at all (http://stackoverflow.com/questions/5981460/optimization-by-java-compiler) – Mat Sep 15 '13 at 11:07
  • You do realize there are two things: comiling into bytecode and runtime optimizing compilation of bytecode into native code? – mikołak Sep 15 '13 at 11:08
  • http://stackoverflow.com/a/15146962/829571 – assylias Sep 15 '13 at 11:50

4 Answers4

3

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).

meriton
  • 68,356
  • 14
  • 108
  • 175
2

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

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.)
Simon
  • 17,223
  • 1
  • 19
  • 23
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.

Avi
  • 21,182
  • 26
  • 82
  • 121