Software Development – Java – Open Source
« Suns Troubleshooting Guide for Java | Back In Business »

Performance Tuning: Object Creation Overhead

Saturday, December 1st, 2007 at 7:53 pm

While working on a Jackrabbit issue I came across a method that is called about a million times for certain requests. In this method an ArrayList instance and a few Integer instances were created on each call to eventually build up a list of ints. I thought I might give it a try and use an int array instead – lo and behold! That gave me an overall performance gain of about 40%.

I wrote this little test code to show the difference:

import java.util.ArrayList;
import java.util.List;


public class ArrayPerformance {

    private static final int LOOP_COUNT = 10000000;
    private static final int ARRAY_SIZE = 10;

    private static void buildArrayList() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < LOOP_COUNT; i++) {
            List values = new ArrayList(ARRAY_SIZE);
            for (int j = 0; j < ARRAY_SIZE; j++) {
                values.add(Integer.valueOf(j));
            }
        }
        long end = System.currentTimeMillis();
        printTime(start, end, "ArrayList");
    }
   
    private static void buildArray() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < LOOP_COUNT; i++) {
            int[] values = new int[ARRAY_SIZE];
            for (int j = 0; j < ARRAY_SIZE; j++) {
                values[j] = j;
            }
        }
        long end = System.currentTimeMillis();
        printTime(start, end, "array");
    }

    private static void printTime(long start, long end, String label) {
        System.out.printf("%-10s: %5dms\n", new Object[]{label, Long.valueOf(end-start)});
    }
   
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            buildArray();
            buildArrayList();
        }
    }
   
}

The resulting output was this:

array     :   475ms
ArrayList :  1200ms
array     :   353ms
ArrayList :  1158ms
array     :   337ms
ArrayList :   921ms
array     :   341ms
ArrayList :   921ms
array     :   341ms
ArrayList :   918ms

Which shows that after some warming up the variant using an ArrayList with Integers to build up a list of ints is roughly about 3 times slower than using a plain int array.

What do I learn from this?

If you know about code sections in your code base which are called millions of times make sure you try to find a solution without creating short living object instances, because even if the current JVM is quite fast creating and collection object instances, it still eats some CPU.
This might lead to solutions that are not object oriented anymore, but this is a small drawback if it gives a performance critical part of your application a speedup.

Another thing is that using natives like ints or longs is always preferable over Integer or Long objects for performance critical code. Lucene for example uses natives a lot to get the best performing code.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply