2

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 E 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:
MergeSort:
Total time 14.333066343496
QuickSort:
Total time 14.3330663435256
HeapSort:
Total time 14.3330663435256 

If you need code snippets just notify me.

Dimitar
  • 4,402
  • 4
  • 31
  • 47
  • 1
    Uh... why not test with a larger array? Even for a micro benchmark, you need to get over a certain threshold of execution time. – Turing85 Nov 17 '15 at 18:33
  • 1
    Please do post code snippets - thanks. – TR1 Nov 17 '15 at 18:33
  • 3
    `System.nanoTime()` has 1000000 times the granularity of `System.currentTimeMillis()`. – Andreas Nov 17 '15 at 18:35
  • @Andreas I'm pretty sure that's OS specific though for accuracy, from Oracle's Java API: `This method provides nanosecond precision, but not necessarily nanosecond accuracy`, but it's probably the most accurate thing available in any case – phflack Nov 17 '15 at 18:53
  • @Andreas That is not generally true: https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#nanoTime-- – Jason Young May 29 '19 at 00:20
  • @JasonYoung Already covered in the [previous comment](https://stackoverflow.com/questions/33764428/how-to-improve-system-currenttimemillis-granularity?noredirect=1#comment55297189_33764428). 2.5 years ago, and I meant "granularity" to mean *precision*, not *accuracy* or *resolution*. as both you and phflack seemed to think. --- Besides, the resolution was greatly increased in Java 9 as part of [JDK-8068730](https://bugs.openjdk.java.net/browse/JDK-8068730). – Andreas May 29 '19 at 00:53

2 Answers2

4

To your direct question, use System.nanoTime() for more granular timestamps.

To your underlying question of how to get better benchmarks, you should run the benchmark repeatedly and on larger data sets. A benchmark that takes ~14ms to execute is going to be very noisy, even with a more precise clock. See also How do I write a correct micro-benchmark in Java?

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
1

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.

(source)

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.

(source)

logoff
  • 3,347
  • 5
  • 41
  • 58