18

This question already has an answer here:

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...


  • 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. - Mangat Rai Modi
  • @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. - Joachim Sauer
  • 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? - Val
  • @Joachim Saucer I understand that. With casual reader I meant just running some quick tests for own understanding. - Mangat Rai Modi
  • @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. - Mangat Rai Modi

6 답변


15

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?


  • The question you mentioned is good. I will read it thoroughly... - Mangat Rai Modi

6

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.



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.


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.



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;


  • I suggest not to use currenttimemillis for knowing execution time - SpringLearner

2

long startTime = System.currentTimeMillis();

//code lines whose time you want to calculate 

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


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

Linked


Related

Latest