1

I am trying to sort an array using multithreading in java and note its execution time. I am using System.nanoTime() for the same.

Here is a template of the code

start = System.nanoTime();
sort();   // calls different threads to completely sort the array
end = System.nanoTime()

What I want to ask is that the above is in main Thread so will it give me incorrect time results given that I did not put main.sleep() ?? or is it handled by the JVM . Also if I am in need of fast execution is there something that can improve the performance of above code in terms of time execution??

2
  • @user506710: what you need is statistical time measurements. On a single sample you can be "unlucky" and have, for example, the OS scheduling out your app or the JVM scheduling out your sorting thread. What you want is to repeat the measurement several times and take, say, the average of 90% of the sampling around the mean or median (in this case nitpicking about using either the mean or the median is unlikely to make any difference). Commented Nov 14, 2010 at 12:37
  • @user506710: btw... Mono-threaded sorting algorithms do suck really big times in today's quad/octo-cores world and I'm pretty sure your sort() is a single-threaded sort. If you have any significant amount of data to sort, replacing it with a multi-threaded sort will improve the performances of the above code big times (sadly the default Java API doesn't provide any). Commented Nov 14, 2010 at 12:40

2 Answers 2

3

What I want to ask is that the above is in main Thread so will it give me incorrect time results given that I did not put main.sleep() ?? or is it handled by the JVM .

The System.nanoTime() is supposed to give you the best (most precise) available measure of elapsed time. Note:

  • this is NOT a measure of the CPU time used, and
  • on some versions of Java / OS platforms, System.nanoTime() uses a hardware clock that is not synchronized across different cores, and (apparently) can be rather inaccurate as a result.

Personally, I'd use System.currentTimeMillis(), and make sure that I benchmarked the sorting of a large array or collection ... and did various other things to make sure that my benchmark was valid.

( EDIT - If you want to find out how much CPU time was used by each thread, take a look the ThreadMXBean API.)

Also if I am in need of fast execution is there something that can improve the performance of above code in terms of time execution??

That is far too general a question to answer, except to say "probably yes" ... or "profile it".

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the JVisualVM profiler (or another profiler) to find how fast each of your methods executes.

All you need is a pause at the beginning to allow you to attach it, and a pause at the end so you can view and/or save the results.

2 Comments

Hey can you tell a little bit more on how to use it. I have jdk 6 and eclipse
You can open it by opening the command line and typing jvisualvm (I've tried this in linux and os x, but not in windows). (I think) you need to add a pause before your app starts and go to jvisualvm, to the profiling tab, and click CPU or Memory. You should also add a pause before your app terminates to allow a good "snapshot" of the results. I recommend using (new Scanner(System.in)).nextLine() or something like that. You can find the bottlenecks easier if you break your app out into smaller methods, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.