代码之家  ›  专栏  ›  技术社区  ›  dfa

Java性能定时库

  •  21
  • dfa  · 技术社区  · 15 年前

    我经常用 System.nanoTime() 配对以计时。类似:

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

    有什么好的定时库可以帮助解决这个问题?也将接受本地代码。

    这里,探查器不是一个解决方案,因为我想在单元测试中强制一些时间约束,所以我想计时方法 以编程方式 .

    12 回复  |  直到 10 年前
        1
  •  11
  •   Mark    15 年前

    我没用过,但我遇到了 perf4j 最近。

        2
  •  8
  •   Manuel Selva    15 年前

    不是您问题的直接答案,但我也经常使用此提示来计时代码,并只编写以下简单的Eclipse->模板环绕:

    long startTime = System.currentTimeMillis();
    ${line_selection}${cursor}
    long totalTime = System.currentTimeMillis() - startTime;
    System.out.println("Total time = " + totalTime);
    System.out.println();
    
        3
  •  8
  •   BenMorel Manish Pradhan    11 年前

    JUnit4具有内置的定时约束功能。

    @测试(超时=x)

    应该有技巧。x是该方法允许运行的最大毫秒数。

        4
  •  6
  •   user802421    10 年前

    StopWatch 从commons lang,它还允许您拆分计时器。

        5
  •  3
  •   Malax    15 年前

    如果你用的是弹簧,你已经有了一个很好的班级 StopWatch 在你的课堂上提出这个建议。

        6
  •  1
  •   techzen    15 年前

    尝试 JPerf ?

        7
  •  1
  •   Eric J.    15 年前

    在这个问题上,你在寻求什么样的帮助?你已经准备好了基础知识。您可以获得以纳秒为单位的经过时间,精确到底层操作系统/硬件能够达到的任何分辨率。

    也。。。我知道你说过不让我做侧写…但我有着杰出的经验 YourKit . 它提供了一个可用于从外部控制分析的API。根据你的具体问题,这个可能值得一看。

        8
  •  1
  •   nicerobot    13 年前
        9
  •  1
  •   Kai    13 年前

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

        10
  •  0
  •   martin    11 年前

    手工制作的。。。

    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;
        }
    }
    
        11
  •  0
  •   keiki    10 年前

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

        12
  •  0
  •   Garret Wilson    10 年前

    我刚开始使用 Java Simon (以前) on Google Code ),看起来棒极了!一组简单的秒表和计数器,几乎没有依赖关系。