; last updated - 1 minute read

If you're like me, you're concerned about your application's performance. So you're familiar with profiling your application.

The simplest way to measure your application's performance is to time how long a method call takes. Most of you probably know System.currentTimeMillis(), but there's a better alternative:

public long measure() { long startTime = System.nanoTime(); work(); return System.nanoTime() - startTime; }

System.nanoTime() is a great function, but one thing it's not: accurate to the nanosecond. The accuracy of your measurement varies widely depending on your operation system, on your hardware and on your Java version. As a rule of thumb, you can expect microsecond resolution (and a lot better on some systems).

Alexey Shipilev wrote a great article on the dos and don'ts of measure performance. Among other things he shows how multithreading can spoil the accuracy of performance measurements, reducing the resolution of System.nanotime() to 15 milliseconds under averse conditions.


Comments