Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中是否有用于测量执行时间的命令?_Java_Command Line_Execution Time - Fatal编程技术网

java中是否有用于测量执行时间的命令?

java中是否有用于测量执行时间的命令?,java,command-line,execution-time,Java,Command Line,Execution Time,java中是否有用于测量执行时间的命令 差不多 System.out.println(execution.time); 在代码末尾。您可以使用以下方法轻松实现: 或者(特别是如果您的任务没有运行那么长的时间),您可能希望使用。请注意,与currentTimeMillis()的工作方式相反,nanoTime()返回的值相对于某个指定的时间是而不是。这意味着nanoTime()只能用于测量时间跨度,而不能用于识别某些特定的时间点。以下是一个关于如何做到这一点的示例: public class Ex

java中是否有用于测量执行时间的命令

差不多

System.out.println(execution.time);

在代码末尾。

您可以使用以下方法轻松实现:

或者(特别是如果您的任务没有运行那么长的时间),您可能希望使用。请注意,与
currentTimeMillis()
的工作方式相反,
nanoTime()
返回的值相对于某个指定的时间是而不是。这意味着
nanoTime()
只能用于测量时间跨度,而不能用于识别某些特定的时间点。

以下是一个关于如何做到这一点的示例:

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");
  }
}
公共类执行计时器{
私人长期启动;
私人长尾;
公共执行计时器(){
重置();
start=System.currentTimeMillis();
}
公共无效结束(){
end=System.currentTimeMillis();
}
公共长期服务(){
返回(结束-开始);
}
公共无效重置(){
开始=0;
结束=0;
}
公共静态void main(字符串s[]{
//简单例子
ExecutionTimer t=新的ExecutionTimer();
对于(int i=0;i<80;i++){
系统输出打印(“.”);
}
t、 end();
System.out.println(“\n”+t.duration()+“ms”);
}
}

您可以运行探查器,或者使用对
System.currentTimeMillis()的两次调用之差

像这样:

long start = System.currentTimeMillis();
....
doSomething();
....
long end = System.currentTimeMillis();

System.out.println("Execution time was "+(end-start)+" ms.");

最简单的方法是在代码执行前后使用System.currentTimeMillis()。Joda Time有更复杂的版本:

如果您想了解更多有关测量内容的详细信息,我强烈建议您使用JMX,尤其是ThreadMXBean:

代码示例:

ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
if (bean.isCurrentThreadCpuTimeSupported()) {
    long cpuTime = bean.getCurrentThreadCpuTime( );
}
long userTime = bean.getCurrentThreadUserTime( );
这里有一个完整的代码示例说明:

使用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);
  }
}

Apache Commons库有类,Spring也有类。

您可以设计一个控件抽象
时间
,它将要执行的操作作为参数,测量并打印执行它所需的时间

代码:

interface Action<A> {
  public A perform();
}

class Timer {
  public static <A> A time(final String description, final Action<A> action) {
    final long start = System.nanoTime();
    final A result = action.perform();
    final long end = System.nanoTime();
    System.out.println(description + " - Time elapsed: " + (end - start) +"ns");
    return result;
  }
}

class Main {
  public static void main(final String[] args) {
    final int factorialOf5 = Timer.time("Calculating factorial of 5",
      new Action<Integer>() {
        public Integer perform() {
          int result = 1;
          for(int i = 2; i <= 5; i++) {
            result *= i;
          }
          return result;
        }
      }
    );
    System.out.println("Result: " + factorialOf5);
  }
}

// Output:
// Calculating factorial of 5 - Time elapsed: 782052ns
// Result: 120
接口动作{
公共表演;
}
班级计时器{
公共静态时间(最终字符串描述、最终操作){
最终长启动=System.nanoTime();
最终结果=action.perform();
最终长端=System.nanoTime();
System.out.println(description+“-经过的时间:”+(end-start)+“ns”);
返回结果;
}
}
班长{
公共静态void main(最终字符串[]args){
final int factorialOf5=Timer.time(“计算5的阶乘”,
新行动(){
公共整数执行(){
int结果=1;

对于(inti=2;i我喜欢RoflcoptrException的类示例。 我将其改写为要点:

public class ExecutionTimer {
  private long start;

  public ExecutionTimer() {
    restart();
  }

  public void restart() {
    start = System.currentTimeMillis();
  }

  public long time(){
    long end = System.currentTimeMillis();
    return (end-start);
  }
  public String toString() {
     return "Time="+time()+" ms";
  }
}

最好使用
System.nanoTime()
来测量经过的时间。@Péter:通常情况下,这种差异不会很明显。我的回答是基于执行时间有点长(即多秒)的假设。我在构造函数中设置了
start
,这使得错误地使用类变得更加困难;-)我认为没有必要使用重置方法。不过这是一个很好的例子。理论上,您应该使用
System.nanoTime()
而不是
System.currentTimeMillis()
,因为currentTimeMillis可以向后移动(当系统时间改变时)。但在大多数情况下,这不是一个真正的问题,因为系统时间很少发生变化。纳米时间测量经过的时间,因此它不受系统时间变化的影响。
public class ExecutionTimer {
  private long start;

  public ExecutionTimer() {
    restart();
  }

  public void restart() {
    start = System.currentTimeMillis();
  }

  public long time(){
    long end = System.currentTimeMillis();
    return (end-start);
  }
  public String toString() {
     return "Time="+time()+" ms";
  }
}