如何计算我的程序的运行时间? [重复]

时间:2021-12-21 20:47:32

Possible Duplicate:
How do I time a method's execution in Java?

可能重复:如何计算方法在Java中的执行时间?

I wrote a program and now I want to calculate the total running time of my program from start to end.

我写了一个程序,现在我想从头到尾计算程序的总运行时间。

How can I do this?

我怎样才能做到这一点?

6 个解决方案

#1


199  

Use System.nanoTime to get the current time.

使用System.nanoTime获取当前时间。

long startTime = System.nanoTime();
.....your program....
long endTime   = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println(totalTime);

The above code prints the running time of the program in nanoseconds.

上面的代码以纳秒为单位打印程序的运行时间。

#2


37  

At the beginning of your main method, add this line of code :

在main方法的开头,添加以下代码行:

final long startTime = System.nanoTime();

And then, at the last line of your main method, you can add :

然后,在main方法的最后一行,您可以添加:

final long duration = System.nanoTime() - startTime;

duration now contains the time in nanoseconds that your program ran. You can for example print this value like this:

duration现在包含程序运行的时间(以纳秒为单位)。例如,您可以像这样打印此值:

System.out.println(duration);

If you want to show duration time in seconds, you must divide the value by 1'000'000'000. Or if you want a Date object: Date myTime = new Date(duration / 1000); You can then access the various methods of Date to print number of minutes, hours, etc.

如果要以秒为单位显示持续时间,则必须将值除以1'000'000'000。或者如果你想要一个Date对象:Date myTime = new Date(duration / 1000);然后,您可以访问日期的各种方法来打印分钟数,小时数等。

#3


15  

Use System.currentTimeMillis() or System.nanoTime() if you want even more precise reading. Usually, milliseconds is precise enough if you need to output the value to the user. Moreover, System.nanoTime() may return negative values, thus it may be possible that, if you're using that method, the return value is not correct.

如果您想要更精确的读取,请使用System.currentTimeMillis()或System.nanoTime()。通常,如果您需要将值输出给用户,则毫秒就足够精确。此外,System.nanoTime()可能返回负值,因此,如果您使用该方法,则返回值可能不正确。

A general and wide use would be to use milliseconds :

一般而广泛的用途是使用毫秒:

long start = System.currentTimeMillis();

... 


long end = System.currentTimeMillis();

NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");

Note that nanoseconds are usually used to calculate very short and precise program executions, such as unit testing and benchmarking. Thus, for overall program execution, milliseconds are preferable.

请注意,纳秒通常用于计算非常短且精确的程序执行,例如单元测试和基准测试。因此,对于整个程序执行,毫秒是优选的。

#4


4  

The general approach to this is to:

一般的方法是:

  1. Get the time at the start of your benchmark, say at the start of main().
  2. 在main()的开头说出基准测试开始时的时间。
  3. Run your code.
  4. 运行你的代码。
  5. Get the time at the end of your benchmark, say at the end of main().
  6. 获取基准测试结束时的时间,比如在main()结束时。
  7. Subtract the start time from the end time and convert into appropriate units.
  8. 从结束时间中减去开始时间并转换为适当的单位。

A hint, look at System.getCurrentTimeMillis() or System.nanoTime().

提示,请查看System.getCurrentTimeMillis()或System.nanoTime()。

#5


2  

You need to get the time when the application starts, and compare that to the time when the application ends.

您需要获取应用程序启动的时间,并将其与应用程序结束的时间进行比较。

Wen the app starts:

应用程序启动文:

Calendar calendar = Calendar.getInstance();

// Get start time (this needs to be a global variable).
Date startDate = calendar.getTime();

When the application ends

当申请结束时

Calendar calendar = Calendar.getInstance();

// Get start time (this needs to be a global variable).
Date endDate = calendar.getTime();

To get the difference (in millseconds), do this:

要获得差异(以毫秒为单位),请执行以下操作:

long sumDate = endDate.getTime() - startDate.getTime();

#6


2  

Beside the well-known (and already mentioned) System.currentTimeMillis() and System.nanoTime() there is also a neat library called perf4j which might be useful too, depending on your purpose of course.

除了众所周知的(已经提到的)System.currentTimeMillis()和System.nanoTime()之外,还有一个名为perf4j的整洁库,它也可能很有用,具体取决于您的目的。

#1


199  

Use System.nanoTime to get the current time.

使用System.nanoTime获取当前时间。

long startTime = System.nanoTime();
.....your program....
long endTime   = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println(totalTime);

The above code prints the running time of the program in nanoseconds.

上面的代码以纳秒为单位打印程序的运行时间。

#2


37  

At the beginning of your main method, add this line of code :

在main方法的开头,添加以下代码行:

final long startTime = System.nanoTime();

And then, at the last line of your main method, you can add :

然后,在main方法的最后一行,您可以添加:

final long duration = System.nanoTime() - startTime;

duration now contains the time in nanoseconds that your program ran. You can for example print this value like this:

duration现在包含程序运行的时间(以纳秒为单位)。例如,您可以像这样打印此值:

System.out.println(duration);

If you want to show duration time in seconds, you must divide the value by 1'000'000'000. Or if you want a Date object: Date myTime = new Date(duration / 1000); You can then access the various methods of Date to print number of minutes, hours, etc.

如果要以秒为单位显示持续时间,则必须将值除以1'000'000'000。或者如果你想要一个Date对象:Date myTime = new Date(duration / 1000);然后,您可以访问日期的各种方法来打印分钟数,小时数等。

#3


15  

Use System.currentTimeMillis() or System.nanoTime() if you want even more precise reading. Usually, milliseconds is precise enough if you need to output the value to the user. Moreover, System.nanoTime() may return negative values, thus it may be possible that, if you're using that method, the return value is not correct.

如果您想要更精确的读取,请使用System.currentTimeMillis()或System.nanoTime()。通常,如果您需要将值输出给用户,则毫秒就足够精确。此外,System.nanoTime()可能返回负值,因此,如果您使用该方法,则返回值可能不正确。

A general and wide use would be to use milliseconds :

一般而广泛的用途是使用毫秒:

long start = System.currentTimeMillis();

... 


long end = System.currentTimeMillis();

NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");

Note that nanoseconds are usually used to calculate very short and precise program executions, such as unit testing and benchmarking. Thus, for overall program execution, milliseconds are preferable.

请注意,纳秒通常用于计算非常短且精确的程序执行,例如单元测试和基准测试。因此,对于整个程序执行,毫秒是优选的。

#4


4  

The general approach to this is to:

一般的方法是:

  1. Get the time at the start of your benchmark, say at the start of main().
  2. 在main()的开头说出基准测试开始时的时间。
  3. Run your code.
  4. 运行你的代码。
  5. Get the time at the end of your benchmark, say at the end of main().
  6. 获取基准测试结束时的时间,比如在main()结束时。
  7. Subtract the start time from the end time and convert into appropriate units.
  8. 从结束时间中减去开始时间并转换为适当的单位。

A hint, look at System.getCurrentTimeMillis() or System.nanoTime().

提示,请查看System.getCurrentTimeMillis()或System.nanoTime()。

#5


2  

You need to get the time when the application starts, and compare that to the time when the application ends.

您需要获取应用程序启动的时间,并将其与应用程序结束的时间进行比较。

Wen the app starts:

应用程序启动文:

Calendar calendar = Calendar.getInstance();

// Get start time (this needs to be a global variable).
Date startDate = calendar.getTime();

When the application ends

当申请结束时

Calendar calendar = Calendar.getInstance();

// Get start time (this needs to be a global variable).
Date endDate = calendar.getTime();

To get the difference (in millseconds), do this:

要获得差异(以毫秒为单位),请执行以下操作:

long sumDate = endDate.getTime() - startDate.getTime();

#6


2  

Beside the well-known (and already mentioned) System.currentTimeMillis() and System.nanoTime() there is also a neat library called perf4j which might be useful too, depending on your purpose of course.

除了众所周知的(已经提到的)System.currentTimeMillis()和System.nanoTime()之外,还有一个名为perf4j的整洁库,它也可能很有用,具体取决于您的目的。