Java logging performance tip
Wrap logging statements inside if then blocks to boost performance.
Applies to log4j (logging in general) and java performance tuning.
Here is a simple tip on tuning a java application. In my own experience I have seen 3x-5x improvement in speed by wrapping logging statements inside a final constant.
log.debug(“application starting”);
becomes:
if(Constants.DEBUG) {
log.debug(“application
starting”);
}
Constants is a separate class in my application with the following directives:
public class Constants {
public static final boolean DEBUG = true;
public static final boolean INFO = true;
public static final boolean WARN = true;
…
}
The performance boost comes from the java compiler dropping non-reachable code. In many situations, debug and info level logging does not need to be enabled on your production build. Chances are, there are many debug and info statements hanging around in the code. Even if logging is set to ERROR level, the debug, info, and warn calls are still being processed. The system has to check what log level it is set at. Efficiency is lost each time it has to work its way through the object model to check.
In production mode, change the Constants to false and recompile:
public class Constants {
public static final boolean DEBUG = false;
public static final boolean INFO = false;
public static final boolean WARN = false;
…
}
Now, the debug, info, and warn statements are no longer being processed! The final constants become literal substitutions at compile time. When they are false, the code inside the if then statements becomes unreachable and is dropped.
|
|
|
Copyright © 1999-2008 Outwardmotion