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.
Ignore this answer as the project is no longer active
I haven't used it but I came across perf4j recently.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With