18

I need to analyze complexity for some algorithms in Java. For that I am planning to give large number of input and measure the time taken by Java implementation. What is the most precise and accurate way to check time between some lines of code? I need precision in milliseconds...

5
  • Not exactly! I was looking for something like System.nanoTime(), however the above mentioned question was more about some formal benchmarking. It requires some deep reading which might not help a casual reader. Commented Nov 26, 2013 at 8:31
  • 2
    @MangatRai: being a "casual reader" is dangerous in this area: almost all cases where you really need to "check time between some lines of code" are heavily influenced by what is discussed in that question. Without that you will get worthless or misleading data and make the wrong decisions. Commented Nov 26, 2013 at 10:06
  • 1
    What your question has to do with the time complexity? Do you understand that time complexity is a static analysis whereas you ask for performance benchmarking? Commented Nov 26, 2013 at 10:15
  • @Joachim Saucer I understand that. With casual reader I meant just running some quick tests for own understanding. Commented Nov 26, 2013 at 11:16
  • @val Time Complexity is indeed a static analysis, but we could estimate it with performance benchmarking. Say on first run I give n elements, then 2n, 3n ... so on. By plotting a graph against time taken I could get an idea, say if complexity is linear or higher polynomial. Surely, we wont get exact time complexity but I asked this more about playing with some code. Commented Nov 26, 2013 at 11:17

6 Answers 6

17

You can get nanosecond resolution, even, using System.nanoTime().

However, you may want to consider the points in the following:

How do I write a correct micro-benchmark in Java?

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

1 Comment

The question you mentioned is good. I will read it thoroughly...
7

Use a library such as Speed4j. This shall not only benchmark the calls but also provide statistics in logs, also you can see them remotely via JMX. It is better of using such a library rather than placing System.current.. calls all over the code.

2 Comments

+1 for adding something new =)
cool! really good addition
5

Say you have a particular method that you would like to put under the microscope. You can do that as follows:

long time1 = System.nanoTime();
thatMethod();
long time2 = System.nanoTime();
long timeTaken = time2 - time1;  
System.out.println("Time taken " + timeTaken + " ns");  

Computers are really fast so it may happen that time difference when using getTimeMillis() maybe zero. Hence, use nanoTime()

You can also use Caliper. They have a video to get started. Plus, thoroughly read the answer pointed to by creichen. It has a lot of great stuff in it.

Comments

4

Use System.nanoTime() or System.currentTimeMillis() to get both start and finish time of your code. Be aware that micro-benchmarks measure only basic aspects of JVM performance. Be aware of warm up phase for JVM, after which JIT is started.

1 Comment

I suggest to use System.nanoTime() stackoverflow.com/questions/11591000/…
2
long start =  System.currentTimeMillis();
// your code
long end = System.currentTimeMillis();
long diff = end-start;

or

long start =  System.nanoTime();
// your code
long end = System.nanoTime();
long diff = end-start;
long diffInMillis = diff/1000000;

1 Comment

I suggest not to use currenttimemillis for knowing execution time
2
long startTime = System.currentTimeMillis();

//code lines whose time you want to calculate 

long endTime = System.currentTimeMillis();
System.out.println("Took "+(endTime - startTime) + " ms");

1 Comment

I am so tempted to -1 this but then.. please format your code. Avoid stating what has already been stated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.