Java Performance Sync Library

I often look at the code in the System.nanoTime () pair to synchronize it. Something like:

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

Is there any good time library that helps with this problem? A home code will also be accepted.

N.B.

The profiler is not a solution here, since I want to use some time limits in my unit tests, so I want to use synchronization methods programmatically .

+21
java performance timing
Aug 6 '09 at 6:00
source share
12 answers

I did not use it, but recently met perf4j .

+11
Aug 6 '09 at 8:44
source share

Not a direct answer to your question, but I also often use this advice at the time of 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(); 
+8
Aug 06 '09 at 6:12
source share

JUnit 4 got a built-in time limit function.

@Test (timeout = X)

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

+8
Aug 6 '09 at 8:25
source share

there is a StopWatch from commons-lang, it also allows you to split the timer.

+6
Aug 6 '09 at 6:27
source share

If you use Spring, you already have a class called StopWatch in your classpath for this sentence.

+3
Sep 10 '09 at 21:27
source share

Tried JPerf ?

+1
Aug 6 '09 at 6:16
source share

What help are you looking for with this problem? You have the basics. You get elapsed time in nanoseconds, exactly depending on what the underlying OS / hardware is capable of.

Also ... and I know that you did not tell the profilers ... but I had an outstanding experience with YourKit . It provides an API that you can use to manage profiling from the outside. Depending on your specific problem, this may be worth a look.

+1
Aug 06 '09 at 6:17
source share

JETM is a good library for this. It can also contain minutes, max and average values, and can also generate informative graphs.

+1
Dec 07 2018-11-12T00:
source share

Caliper

And besides reading the wiki on this site, I recommend reading:

+1
Dec 07 '11 at 13:32
source share

handmade...

 import static java.lang.System.nanoTime; /** * For testing / debug purposes. * * <pre> * private Stopwatch stopwatch = new Stopwatch(); * ... * public void method1() { * stopwatch.reset(); * for (...) { * ... * stopwatch.start(); * operation1(); * stopwatch.stop(); * ... * } * System.out.println("operation 1 max timing is " + stopwatch.getMaxLapTime()); * } * ... * public void method2() { * stopwatch.reset(); * for (...) { * ... * stopwatch.start(); * operation2(); * stopwatch.stop(); * ... * } * System.out.println("operation 2 max timing is " + stopwatch.getMaxLapTime()); * } * </pre> * * @author Mykhaylo Adamovych */ public class Stopwatch { protected long totalTime; protected long maxLapTime; protected long minLapTime = Long.MAX_VALUE; protected long lapsCount; protected long lastThreshold; /** * Human readable time in seconds * * @param nanoTime * @return time in seconds */ public static final String toSeconds(long nanoTime) { return String.format("%.9f", nanoTime / 1000000000.0); } public long getAverageLapTime() { if (lapsCount == 0) return 0; return totalTime / lapsCount; } public long getMaxLapTime() { return maxLapTime; } public long getMinLapTime() { return minLapTime; } public long getTotalTime() { return totalTime; } /** * Returns last lap time, process statistic. */ public long lapTime() { return processLapTime(); } private long processLapTime() { if (lastThreshold == 0) throw new IllegalStateException("Stopwatch is stopped."); final long now = nanoTime(); final long lapTime = now - lastThreshold; lapsCount++; totalTime += lapTime; if (lapTime > maxLapTime) maxLapTime = lapTime; if (lapTime < minLapTime) minLapTime = lapTime; lastThreshold = nanoTime(); return lapTime; } /** * Resets statistic and starts. */ public void reset() { totalTime = 0; maxLapTime = 0; minLapTime = Long.MAX_VALUE; lapsCount = 0; start(); } /** * Starts time watching. */ public void start() { lastThreshold = nanoTime(); } /** * Suspends time watching, returns last lap time. */ public long stop() { final long lapTime = processLapTime(); lastThreshold = 0; return lapTime; } } 
0
May 17 '12 at 12:10
source share

Something new on the JMH market. This is done under the auspices of the openjdk project.

0
Sep 25 '14 at 2:06
source share

I just started using Java Simon (previously in Google Code ) and it seems awesome! A simple set of stopwatch and counters with several dependencies.

0
Nov 02 '14 at 23:36
source share



All Articles