tags:

views:

295

answers:

3

I want to compare the performance of certain operations in my application.

Other than using the Date object, is there anything more precise?

+4  A: 

public static long nanoTime() - Returns the current value of the most precise available system timer, in nanoseconds.

My guess is that since System.nanoTime() uses the "most precise available system timer" which apparently only has millisecond-precision on your system, you can't get anything better.

Yada
No, it hasn't only millisecond precision. They access the much more accurate media timer in Java if possible with much more resolution.
Thorsten S.
+1  A: 

If you're measuring elapsed time by subtracting two timestamps, you should use System.nanoTime() to get those timestamps. That's what it's for.

Kevin Bourrillion
A: 

To get the CPU time you can use the getCurrentThreadCpuTime of the Thread Management Bean.
It returns the CPU time used by the actual thread in nanoseconds:

    ThreadMXBean threadMX = ManagementFactory.getThreadMXBean();
    long time = threadMX.getCurrentThreadCpuTime();

    // do something 

    time = threadMX.getCurrentThreadCpuTime() - time;  // CPU time in nanoseconds

check the documentation for details and some problems like CPU time measurement not being enabled.

Carlos Heuberger