user8 user8 - 1 year ago 92
Java Question

How to improve System.currentTimeMillis() granularity?

How can I achieve it without giving as input very large arrays? I am measuring the running time of different algorithms and for an array of 20 elements I get very (the same) similar values. I tried divided the total time by 1000000000 to clear of the

and then used like 16 mirrors where I copied the input array and executed it again for the mirror. But still it is the same for Heap and Quick sort. Any ideas without needing to write redundant lines?

Sample output:

Random array:
Total time 14.333066343496
Total time 14.3330663435256
Total time 14.3330663435256

If you need code snippets just notify me.


You can't improve the granularity of this method. According to Java SE documentation:

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.


As others stated, for time lapses, public static long nanoTime() would give you more precision, but not resolution:

This method provides nanosecond precision, but not necessarily nanosecond resolution.