Java Code Optimization
Strength Reduction
Use light operator instead of heavy operator. Use of short hand notation is encouraged.
All these short hands will be useful when such expression are used in loop or are used as indices of array
Common Sub-Expression elimination
Common sub expression elimination removes redundant calculation
Code Motion
In a loop never write expressions which are invariant. An invariant expressions are those which won’t be evaluated each time loop is iterated.
Compiler Optimizations
After finishing development and testing the code. Recompile with compiler optimizations turned on. Recompile code using option javac -o
Input/output: Use BufferedInputStream and BufferedOutputStream or equivalent buffered methods wherever possible; doing I/O a single byte at a time is generally too slow to be practical. Note that the JDK 1.0.2 I/O classes use lots of synchronization, so you might get better performance by using a single "bulk" call such as readFully and then interpreting the data.
Synchronization: In the JDK interpreter, calling a synchronized method is typically 10 times slower than calling an unsynchronized method. With JIT compilers, this performance gap has increased to 50-100 times (see Java microbenchmarks). Avoid synchronized methods if you can -- if you can't, synchronizing on methods rather than on code blocks is slightly faster.
Know your switches: A switch statement is compiled into one of two byte codes, depending on the scarcity of the cases you're switching on. The first, where the numbers are close together, uses a fast direct lookup. The second, where the numbers are further apart, uses a slower search through a table. Look at the bytecode your code is compiled into to find which you're using. This is particularly important if you're trying to replace a sequence of if statements with a switch.
String: The String concatenation operator + looks innocent but involves a lot of work: a new StringBuffer is created, the two arguments are added to it with append(), and the final result is converted back with a toString(). This costs both space and time. In particular, if you're appending more than one String, consider using a StringBuffer directly instead.
Services: - Java Code Optimization Homework | Java Code Optimization Homework Help | Java Code Optimization Homework Help Services | Live Java Code Optimization Homework Help | Java Code Optimization Homework Tutors | Online Java Code Optimization Homework Help | Java Code Optimization Tutors | Online Java Code Optimization Tutors | Java Code Optimization Homework Services | Java Code Optimization