Java - Big result difference when benchmarking an algorithm in ms and ns?

The second call will be extremely fast if the list is already sorted.

– user393381 Dec 8 '10 at 21:54 2 @user393381: No, don't delete it! If somebody has the same problem and googles for it, he can find help here. – thejh Dec 8 '10 at 21:55 @user: Also you were given a correct answer, so you can just accept it and it will be "closed off" (in that nobody will try to answer it) – Mark Peters Dec 8 '10 at 21:56 Also note that the second time you sort the list, even if it is reset, the code will be all loaded and ready to go.

– Tom Hawtin - tackline Dec 8 '10 at 23:40.

Depending on what platform you're on, System.nanoTime() itself can be very slow. You're better off running your benchmark multiple times and measuring the overall duration in miliseconds.

1 But they're getting the opposite result. More time in ms than ns – dspyz Dec 8 '10 at 22:41 @dspyz I know that. The point I'm making (badly) is that it's better to measure a longer time (by looping the benchmark) with a more granular unit, than to try and measure a very short interval, using a measure which doesn't always behave predictably.

– developmentalinsanity Dec 8 '10 at 22:46 W its also worth nothing that currentTimeMillis() and nanoTime() take the same amount of time on some platforms. Its worth following your suggestion no matter how you measure time. – Peter Lawrey Dec 8 '10 at 23:09.

I suggest you run the test for at least 2 seconds before counting any result and run the test for at least 2 seconds and take the average. The following code prints. Average sort time 116 ms.

Average sort time 117100526 ns. Average sort time 116 ms. Average sort time 116530255 ns.

Average sort time 117 ms. Average sort time 116905977 ns. Code public static void main(String... args) throws IOException { String strings = new String100 * 1000; for (int I = 0; I 0) System.out.

Println("Average sort time " + time / count + " ms. "); } { long start = System.nanoTime(); int count = 0; do { Arrays. Sort(strings.clone()); count++; } while (System.nanoTime() - start 0) System.out.

Println("Average sort time " + time / count + " ns. "); } } }.

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