0

I wish to measure the time taken to instantiate an object in java. I can't use the profiling tool in Eclipse as it won't work for me - I get a nasty error. Is it possible to measure the time manually? I have the following

    for (int i=0; i<1000; i++) {
        long endTime;
        long startTime = System.nanoTime();
        MyObj obj = new MyObj();
        endTime = System.nanoTime();
        System.out.println("Instantiation time: " + (endTime - startTime));
    }

The problem with this however is that it just returns 0 all the time. Any suggestions?

Joeblackdev
  • 7,217
  • 24
  • 69
  • 106
  • 1
    For operations that take tiny a amount of time, it's often beneficial to to perform them thousands of times (or more, if necessary), measure the whole block of time, and then divide by the number of iterations to get an approximation. – dlev Aug 08 '11 at 20:41
  • You are still just measuring each individual instantiation. You need to move the start and end time calculations *outside* the loop. – dlev Aug 08 '11 at 20:48

2 Answers2

5

Instead use System.nanoTime() - you'll get more precision with it (if your VM and hardware support it).

Paul
  • 19,704
  • 14
  • 78
  • 96
  • Most certainly that won't help. System.nanoTime still has only about 10-15ms (on Windows, no idea about Linux) which is way more than a simple object instantiation will usually take. Still a good idea to use nanoTime() but not without calling the constructor a few hundred or thousand times. – Voo Aug 08 '11 at 21:39
  • 2
    Source for your claim, please? [This SO answer](http://stackoverflow.com/questions/510462/is-system-nanotime-completely-useless/4588605#4588605) indicates otherwise, and references a blog post by the Sun/Oracle realtime/concurrency guy. – Paul Aug 08 '11 at 21:46
1

I think instantiating an object is just really really really fast. Why don't you try the time to instantiate one million objects in a loop and then divide the total time by one million. You might get a more usable number.

public class TimeTest {
    private final int LOOPCOUNT = 100000;

    @Test
    public void CreateObj() {                       
        long endTime;
        long startTime = System.nanoTime();     
        for (int i=0; i<LOOPCOUNT; i++) {
            MyObj obj = new MyObj();
        }
        endTime = System.nanoTime();
        System.out.println("Total loop time      : " + (endTime - startTime) + "ns");
        System.out.println("Avg single loop time : " + (endTime - startTime) / LOOPCOUNT + "ns");
    }

    class MyObj {       
    }   
}

On my machine I get:

Total loop time      : 2341515ns
Avg single loop time : 23ns

And as @bdares points out, you can measure the time to run the loop empty and then subtract it from your total loop time to get a more accurate number.

codemonkey
  • 1,213
  • 2
  • 14
  • 31
  • Probably want to measure time for an empty loop that iterates a million times. –  Aug 08 '11 at 20:46
  • There's a good chance the JIT is used for any large loop. And that'll certainly optimize an empty loop away, so that won't work. But then the overhead is fixed and relatively low - shouldn't matter that much. – Voo Aug 08 '11 at 21:40