javac optimization flags

themaestro picture themaestro · Feb 14, 2011 · Viewed 31.6k times · Source

I've recently been writing a lot of code in C and am now switching over to Java. I'm currently implementing a large data structure and was wondering whether there were any optimization flags I can turn on when I invoke the Java compiler in order to improve performance like in gcc.

I'm used to:

gcc -O3 -NDEBUG MyProgram.c

is there an analogous command for javac?

I'm using JDK and am running Ubuntu 10.04.

Answer

Péter Török picture Péter Török · Feb 14, 2011

Optimization in Java is mostly done by the JIT compiler at runtime. Hence there is no point trying to instruct it to optimize a certain way at compile time (when it is creating only bytecode anyway). The JIT will almost surely make better decisions on the spot, knowing the exact environment and observing the actual patterns of execution of specific parts of your code.

There are some specific compiler options affecting performance, but these are for tuning the JVM (including the garbage collector) rather than optimizing the code itself.