Example -
Vector tempVector =methodReturningVector();
//Wrong
for(int i=0;i<tempVector.size();++i ) {
// loop body
}
The reason this example is incorrect , because it is invoking size() method of Vector class as a terminating condition. If this vector has 100 elements, this method will be called 100 times. Though size() method itself is not a heavy method, but calling any method has a lot of overheads itself, which can degrade the performance of the program.
A better implementation will be -
Vector tempVector =methodReturningVector();
//better
int ctr=tempVector.size();
for(int i=0;i<ctr;++i ){
// loop body
}
Tip 2 - Don't use new operator to create a String literals.
Example
//do not do this
String myString = new String("Hello");
Better practice would be
//better code
String myString ="Hello";
Reason :
If you use the new operator to create a String, a new String object will be created. However, if you use the second approach, in that case, this String literal will be interned i.e. different String literals with same value will point to same reference.
In the declaration of a field, setting it explicitly to its default initial value is always redundant, and may even cause the same operation to be performed twice (depending on your compiler). |
Example :
Declaring and initializing object fields as null, for instance, is simply not necessary in Java. |
public final class Quark { //WITH redundant initialization to default values private String fName = null; private double fMass = 0; public Quark(String aName, double aMass){ fName = aName; fMass = aMass; } } The correct approach would be - public final class Quark { //WITHOUT redundant initialization to default values private String fName ; private double fMass ; public Quark(String aName, double aMass){ fName = aName; fMass = aMass; } } |
If you look at the bytecode (using javap) generated for both of these approaches, you can see that the bytecode generated for first case has many duplicate operations.
Tip 4 -Use Local variables instead of member fields/ instance fields wherever possible.
Reason
Local variables cost less than instance variables. The JVM knows the exact location of local or stack variables at compile time. However, the JVM performs lookups to determine the location of an object’s instance variables. This lookup is very expensive.
Example
// The use of instance variables
int i, j;
public void method1
{
for(i=0; i<10000; i++)
{
j += i;
}
}
// use of local variables
public void method2
{
int temp;
for(int k=0; k<10000; k++)
{
temp += k;
}
j = temp;
}
Method method2() is much faster than method method1() because it uses local variables as a loop counter and to store the temporary result before assigning it to the class instance variable after the loop. The JVM stores temp, a local variable, on the stack and has quick access to it. On the other hand, the JVM stores j, an instance variable, on the heap and requires a lookup on each usage. Thus, every time temp is used, it can be accessed much quicker than j, an instance variable stored on the heap. Similarly, using k as a loop counter instead of i also gives faster performance.
Tip 5 - When using arrays it is always efficient to copy arrays using System.arraycopy() than using a loop.
Reason
Because System.arraycopy() is a native method, it is much faster than using loop for copying arrays specially large arrays.
0 comments:
Post a Comment