1. In the important cycle, the elimination of cyclic termination judgment when the method call
Copy Code code as follows:
for (int i=0; i<collection.size (); i++)
{
...
}
for (int i=0; i<collection.size (); i++)
{
...
}
To replace with ...
Copy Code code as follows:
View Plaincopy to Clipboardprint?
for (int i=0;n=collection.size (); i<n;i++)
{
...
}
2. Usually, move the loop index irrelevant to the outside of the loop
Copy Code code as follows:
for (int i=0;terminal=x.length;i<terminal;i++) {
X[i]=x[i]/scalea*scaleb;
}
for (int i=0;terminal=x.length;i<terminal;i++) {
X[i]=x[i]/scalea*scaleb;
}
To
Copy Code code as follows:
Double scale = Scaleb/scalea;
for (int i=0; terminal=x.length; i<terminal; i++) {
X[i]=x[i]*scale;
}
2. String
Eliminate string concatenation
Always use Stringbuffter instead of string when creating long strings
Pre-allocating StringBuffer space
StringBuffer sb = new StringBuffer (5000);
3. Basic data types
Use basic data types in important loops (int-type data is usually faster than long/double data)
The wrapper class for the base data type (BOOLEAN,INTEGER,ETC) is primarily used when the method parameter passed must be a reference to an object (not a basic data type)
Use the static final modifier for all constant algebraic expressions
One makes constants easier to reference (the compiler computes constant expressions in advance)
4. Abnormal
Exceptions are only used for a single true error condition
It is expensive to throw an exception and execute a catch code block (mainly because you want to get a snapshot of the line stacks when an exception is created)
An exception is thrown when a condition is really an exception
The compiler and runtime are optimized to put several method calls in a Try/catch block instead of implementing several Try/catch blocks for each method call
5. Benchmark
Note that all of these techniques will vary depending on the platform and the virtual machine
One example: In some servlet containers, it is faster to pass a outputstream as a byte output
One in the other container, the output character through a printwriter will be faster
These techniques describe the most portable recommendations.
You may need to run some benchmarks to determine what's the fastest on your platform.