Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java performance timing library [closed]

I frequent wrap code in a System.nanoTime() pair in order to timing it. Something like:

long start = System.nanoTime();    
methodToBeTimed();
long elapsedTime = System.nanoTime() - start;

There is any good timing library that helps with this problem? Also homegrown code will be accepted.

NB

A profiler is not a solution here, since I want to enforce some time constraints in my unit tests, so I want to timing methods programmatically.

like image 983
dfa Avatar asked Aug 06 '09 06:08

dfa


3 Answers

Ignore this answer as the project is no longer active

I haven't used it but I came across perf4j recently.

like image 124
Mark Avatar answered Oct 26 '22 17:10

Mark


Not a direct answer to your question, but I am also often using this tip to time my code and just wrote the following simple Eclipse -> Surround With template:

long startTime = System.currentTimeMillis();
${line_selection}${cursor}
long totalTime = System.currentTimeMillis() - startTime;
System.out.println("Total time = " + totalTime);
System.out.println();
like image 9
Manuel Selva Avatar answered Oct 26 '22 17:10

Manuel Selva


JUnit 4 got a built-in timing-contraint functionality.

@Test(timeout=X)

should do the trick. X is the maximum number of milliseconds the method is allowed to run.

like image 8
Philipp Avatar answered Oct 26 '22 19:10

Philipp