Java性能计时库

我经常在System.nanoTime()对中包装代码以便对其进行计时。 就像是:

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

有什么好的计时库可以解决这个问题吗? 还将接受自行开发的代码。

NB

分析器在这里不是解决方案,因为我想在unit testing中强制执行一些时间限制,所以我想以编程方式对方法进行计时。

我没有使用它,但我最近遇到了perf4j 。

不是你的问题的直接答案,但我也经常使用这个技巧来计算我的代码,并编写了以下简单的Eclipse – > Surround With模板:

 long startTime = System.currentTimeMillis(); ${line_selection}${cursor} long totalTime = System.currentTimeMillis() - startTime; System.out.println("Total time = " + totalTime); System.out.println(); 

JUnit 4具有内置的时序约束function。

@Test(超时= X)

应该做的伎俩。 X是允许该方法运行的最大毫秒数。

来自commons-lang的StopWatch ,它还允许你拆分计时器。

如果您正在使用Spring,那么您已经在类路径中为此建议提供了一个名为StopWatch的好类。

试过JPerf ?

你在寻找这个问题有什么帮助? 你有基础知识。 您将获得以纳秒为单位的经过时间,精确到基础OS /硬件能够达到的任何分辨率。

还……我知道你说过没有个人资料……但我在使用YourKit时有过丰富的经验。 它提供了一个API,您可以使用它来控制外部的分析。 根据您的确切问题,这个可能值得一看。

卡尺

除了在该网站上阅读wiki,我建议阅读:

  • “编写Java微基准”
  • “Java理论与实践:有缺陷的微基准的剖析”

JETM是一个很好的库。 它还可以提供分钟,最大值和平均值,还可以生成信息图。

手工制作的…

 import static java.lang.System.nanoTime; /** * For testing / debug purposes. * * 
 * 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()); * } * 

* * @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; } }

JMH是市场上的新东西。 它是在openjdk项目的保护下制作的。

我刚开始使用Java Simon (以前在Google Code上 ),看起来很棒! 一组简单的秒表和计数器,具有很少的依赖关系。