有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (5) 个答案

  1. # 1 楼答案

    以下是关于如何做到这一点的complete and little modified example

    public class ExecutionTimer {
      private long start;
      private long end;
    
      public ExecutionTimer() {
        reset();
        start = System.currentTimeMillis();
      }
    
      public void end() {
        end = System.currentTimeMillis();
      }
    
      public long duration(){
        return (end-start);
      }
    
      public void reset() {
        start = 0;  
        end   = 0;
      }
    
      public static void main(String s[]) {
        // simple example
        ExecutionTimer t = new ExecutionTimer();
        for (int i = 0; i < 80; i++){
    System.out.print(".");
    }
        t.end();
        System.out.println("\n" + t.duration() + " ms");
      }
    }
    
  2. # 2 楼答案

    使用ThreadMXBean获取更详细的计时信息:

    public class Timer {
    
      static { 
        // needed to request 1ms timer interrupt period 
        // http://discuss.joelonsoftware.com/default.asp?joel.3.642646.9
        Thread thread = new Thread(new Runnable() {
          @Override
          public void run() {
            try {
              Thread.sleep(Integer.MAX_VALUE);  (Windows NT)
            } catch (InterruptedException ignored) {
            }
          }
        });
        thread.setName("Timer");
        thread.setDaemon(true);
        thread.start();
      }
    
      private final ThreadMXBean threadMX = ManagementFactory.getThreadMXBean();
      private final long elapsedStart;
      private final long cpuStart;
      private final long userStart;
    
      public Timer() {
        cpuStart = threadMX.getCurrentThreadCpuTime();
        userStart = threadMX.getCurrentThreadUserTime();
        elapsedStart = System.nanoTime();
      }
    
      public void times() {
        long elapsed = elapsedStart - System.nanoTime();
        long cpu = cpuStart - threadMX.getCurrentThreadCpuTime();
        long user = userStart - threadMX.getCurrentThreadUserTime();
        System.out.printf("elapsed=%-8.3f cpu=%-8.3f user=%-8.3f [seconds]", 
                elapsed/1.0e9, cpu/1.0e9, user/1.0e9);
      }
    }
    
  3. # 3 楼答案

    您可以运行一个探查器,或者使用两个调用的差异来System.currentTimeMillis()

    像这样:

    long start = System.currentTimeMillis();
    ....
    doSomething();
    ....
    long end = System.currentTimeMillis();
    
    System.out.println("Execution time was "+(end-start)+" ms.");
    
  4. # 4 楼答案

    最简单的方法就是使用这个系统。currentTimeMillis()在代码执行之前和之后。Joda Time有更复杂的版本:http://joda-time.sourceforge.net/

  5. # 5 楼答案

    如果您想了解更多有关测量内容的详细信息,我强烈建议您使用JMX,尤其是ThreadMXBean:http://download.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html

    代码示例:

    ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
    if (bean.isCurrentThreadCpuTimeSupported()) {
        long cpuTime = bean.getCurrentThreadCpuTime( );
    }
    long userTime = bean.getCurrentThreadUserTime( );
    

    这里有一个完整的代码示例说明: http://nadeausoftware.com/articles/2008/03/java_tip_how_get_cpu_and_user_time_benchmarking