How can I measure time with microsecond precision in Java?

It's not clear to me exactly what you're benchmarking, but in general any test which takes such a short amount of time to run, that accuracy lower than 50 ms is relevant, is going to be very prone to other disturbances.

It's not clear to me exactly what you're benchmarking, but in general any test which takes such a short amount of time to run, that accuracy lower than 50 ms is relevant, is going to be very prone to other disturbances. I generally try to make benchmarks run for at least 10 seconds. The framework I'm writing at the moment will guess how many iterations to run so that it will take 30 seconds.

That means you won't get radically different results just because some other process stole the CPU for a few milliseconds. Running for longer is almost always a better approach than trying to measure with finer-grained accuracy.

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.

You have to repeat the tests thousands of times. There are lots of things happening that will influence your measurements, like garbage collection, I/O, swap in/out, the size of the ready queue threads, etc.

That's weird. System.nanoTime() is supposed to work. Are you using the Sun JVM?

Can you just repeat your operation 1000 times and divide the time by 1000 to find out what you need to know?

System.nanoTime() uses a counter in the CPU and is usually accurate to about 1 micro-second on Windows XP and Linux. Note: Windows XP is often less accurate on multi-cpu machines as it doesn't compensate for different CPUs having different counters. Linux does.

Note 2: It will drift relative to the System. CurrentTimeMillis() as it is based on the accuracy of the clock for your CPU (which doesn't need to be so accurate over a period of time), rather than the clock you have for getting the time.(which drifts less per day, but has less granularity) In your benchmark you are basically testing the speed at which you can create new objects. Not surprisingly your results will vary dramatically based on your GC settings and how recently a GC has been performed.

Try running your tests with the following options and you should see very different results. -verbosegc -XX:NewSize=128m -mx256m.

It may be the case that the underlying OS doesn't provide timers with nanosecond precision. There is also an older post.

If you want a reliable result, use a profiler. I suggest VisualVM, which is easy to install and is bundled with the JDK starting from version 1.6.0_07. It is an easy to use visual tool that integrates several commandline JDK tools and lightweight profiling capabilities.

Such a benchmark that relies on short time-interval gives you unreliable results. You will always get different results, because of external factors like I/O, Swapping, Process Switches, Caches, Garbage Collection etc. Additionally the JVM optimizes your calls, so it's likely that the first measured things are going slower than later call. The JVM starts more and more to optimize the commands you execute.

Additionally the method like System.nanoTime() is dependent on the timers of the underlying system. They may (and most likely will) not have the granularity to measure in that accuracy.To cite the API: This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change.

To really measure with high precision you need to access an external timing hardware with guaranteed URL2 make your benchmark more stable you need to execute it more than once and to measure bigger time-intervals than only milliseconds.

Yes, the accuracy and precision of System. NanoTime is usually much better than System. CurrentTimeMillis, but without guarantee: it can become just as bad in the worst case.ThreadMXBean.

GetCurrentThreadCpuTime tends to yield smaller times, but its resolution is unclear, and it has further disadvantages (do you really want the CPU time? , platform dependent semantics, supported on your platform? ).

Measuring the time with all three techniques also has some cost, i.e. Requires time itself, which can distort the measurements. The costs are highly platform dependent, but often cost(System.

CurrentTimeMillis) About micro benchmarking in general, see How do I write a correct micro-benchmark in Java? Create quick/reliable benchmark with java?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions