Believe or not your code has 90% of time spent in 10% of code. Usually I am tuning on hot spots identified by he profiler. I recommand the NetBean 5.5 profiler. But except that I still see many programmers’s code has “Code Smell” that causes performance slow or memory consuming. So I am going to list some tips here and more examples in the future.

No. 1
Bad:

static String append(String[] array) {
   String result = "";
   for (int i = 0; i < array.length; i++) {
      result = result + array[i];
   }
   return result;
}

Good:

static String append(String[] array) {
  StringBuffer result = new StringBuffer();
  for (int i = 0; i < array.length; i++) {
    result.append(array[i]);
  }
  return result.toString();
}

Tip: Using StringBuffer instead creating new String.

No. 2
Bad:
for (int i = 0; i < array.length; i++) { … }

Good:
int length = array.length;
for (int i = 0; i < length; i++) { … }

Tip: Caching values in local variables can therefore produce a significant improvement.

No. 3
Tip: using Flyweight Pattern
The pattern here states about a mechanism by which you can avoid creating a large number of object instances to represent the ntire system. To decide if some part of your program is a candidate for using Flyweights, consider whether it is possible o remove some data from the class and make it extrinsic. If this makes it possible to reduce greatly the number of different lass instances your program needs to maintain, this might be a case where Flyweights will help.

Ref: Desing Flyweight Pattern

No. 4
Tip: Cache Frequently Computed Values

public final class String {
   private int hash = 0;
   public int hashCode() {
      int h = hash;
      if (h == 0) {
        ...
        hash = h;
      }
      return h;
   }
}

Ref: Cache 101

No. 5
Tip: Recycle your code by object pooling

class ObjectPool {
   private Class objectClass; // Class of objects managed by this factory
   PooledObject freeObjects; // List of previously used objects

   public ObjectPool(Class objectClass) {this.objectClass = objectClass; }

   public PooledObject get() throws InstantiationException, IllegalAccessException {
      PooledObject pooledObject = null;
      if (freeObjects == null) {
         pooledObject = (PooledObject) objectClass.newInstance(); // Allocate a new object
      } else {
         pooledObject = freeObjects; // Get an existing object from the free list
         freeObjects = freeObjects.nextFree;
      }
      pooledObject.factory = this; pooledObject.nextFree = null;
      return pooledObject;
   }
}
abstract class PooledObject {
   PooledObject nextFree; // Next object on free list
   ObjectPool factory;
   void free() {
      nextFree = factory.freeList; // Add object to free list
      factory.freeList = this;
   }
}

No. 6
Tip: Reflection Recommendations
* Avoid reflection if possible
* Define interfaces for extension classes
* Automatic code generation
* Do lookups once and cache for java.lang.reflect.Method object

Advertisements