Measuring Java execution time, memory usage and CPU load for a code segment

后端 未结 4 484
温柔的废话
温柔的废话 2020-12-04 19:36

For a particular segment of Java code, I\'d like to measure:

  • Execution time (most likely thread execution time)
4条回答
  •  旧时难觅i
    2020-12-04 20:24

    Profiling may be an easier option since you don't require in-production stats. Profiling also doesn't require code modification. VisualVM (which ships w/ the JDK 1.6.06+) is a simple tool. If you want something more in-depth I'd go with Eclipse TPTP, Netbeans profiler, or JProfiler(pay).

    If you want to write you own, consider the following:

    Simple measurments like execution time can be done by "clocking" the section you're interested in:

    long start = System.nanoTime(); // requires java 1.5
    // Segment to monitor
    double elapsedTimeInSec = (System.nanoTime() - start) * 1.0e-9;
    

    You can use a similar technique to monitor memory via Runtime.getRuntime().*memory() methods. Keep in mind that tracking memory usage in a garbage collected environment is trickier than simple subtraction.

    CPU load is hard to measure in Java, I typically stick with execution time and optimize the longer / repetitive sections

提交回复
热议问题