tags:

views:

146

answers:

2

My experience shows that object creation in Java is very slow. Often, I optimize my code by just removing object creation and using the same objects repeatedly.

I am wondering if its similarly slow in other language with OOP base, because, to me, it is very counter intuitive that a language that's hardcore object oriented, takes so much time for object creation.

Has anybody profiled this on several langauges?

+5  A: 

While there might be a noticeable CPU impact under rare circumstances, in general object creation for current virtual machines is as fast as it can practically be.

Here is an article I like called "Urban performance legends", I think this is just one of those myths.

Also take a look at http://www.javaperformancetuning.com/tips/rawtips.shtml for ideas of how to performance tuning your Java App.

Regards.

StudiousJoseph
+6  A: 

Try see what you get from this - some exercising to try to get the VM to not optimize the loop away:

import java.util.ArrayList;
import java.util.Random;

class DummyInteger {
    private int i;
    public DummyInteger(int i) {
        this.i = i;
    }
    public int getI() {
        return i;
    }
}

public class ObjectCreationTest {

    final static int ITERATIONS = 2000000;
    public static void main(String[] args) {
        Random r = new Random(System.currentTimeMillis());

        int[] randArr = new int[ITERATIONS];
        for(int i = 0; i < ITERATIONS; i++) {
            randArr[i] = r.nextInt();
        }
        DummyInteger[] iArr = new DummyInteger[ITERATIONS];
        long start = System.currentTimeMillis();
        for(int i = 0; i < ITERATIONS; i++) {
            ArrayList<DummyInteger> list = new ArrayList<DummyInteger>(); 
            list.add(new DummyInteger(randArr[i]));
            iArr[i] = list.get(0);
        }
        long end = System.currentTimeMillis();
        System.out.println(String.format("Creating %d objects took %d ms",ITERATIONS*2,end-start));
        int sum = 0;
        for(DummyInteger mi: iArr) {
            sum += mi.getI();
        }
        System.out.println(sum);
        }
}

I got

Creating 4000000 objects took 678 ms

Which should be around 170 nanosecond per object.

nos
I replaced the arrays with arrays of size 8, keeping ITERATIONS the same and using `% 8` in the index expressions. Then moved the code into a method that I called a number of times. This gave me an asymptotic time of 97ms to create 400,000 objects. That is 24 nanoseconds per object.
Stephen C
Also bear in mind that each ArrayList instance actually contains a Object[], so we are really creating 3 objects per iteration.
Stephen C
I believe that the reason for the speed up when I reduce the array size is that we've *dramatically* reduced the number of objects that the GC has to trace. In other words, most of the time in the original benchmark was running the GC ... and that's clearly not we were trying to measure. Java micro-benchmarks are HARD to get right!!!
Stephen C
+1 - Thanks @nos
euphoria83
It's indeed very hard. Just be careful moving stuff too much around. My initial attempts ran in no-time, the optimizer was too smart and figured out ways to optimize parts of my loops just ocmpletely away.
nos