回答
这里是你如何能做到这一点一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");
}
}
您可以轻松实施自己使用System.currentTimeMillis()
:
final long start = System.currentTimeMillis();
executeLongRunningTask();
final long durationInMilliseconds = System.currentTimeMillis()-start;
System.out.println("executeLongRunningTask() took " + durationInMilliseconds + "ms.");
或者(特别是如果你的任务不运行长),您可能需要使用System.nanoTime()
。请注意,与currentTimeMillis()
的工作方式相反,nanoTime()
返回的值相对于某个指定时间为而不是。这意味着nanoTime()
只能用于测量时间跨度,并且不能用于识别某个特定时间点。
最好使用'System.nanoTime()'来测量经过时间。 – 2011-03-29 11:26:05
@佩特:往往不是,差异不会引人注目。我回答的基础是执行时间有点长的假设(即多秒)。 – 2011-03-29 11:31:06
你可以运行一个分析器,或者使用两个电话的差异System.currentTimeMillis()
像这样:
long start = System.currentTimeMillis();
....
doSomething();
....
long end = System.currentTimeMillis();
System.out.println("Execution time was "+(end-start)+" ms.");
最简单的方法是在执行代码之前和之后使用System.currentTimeMillis()。乔达时有更复杂的版本:http://joda-time.sourceforge.net/
如果你想对你测量的东西更多的细节,我强烈建议你使用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
使用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);
}
}
您可以设计一个控制抽象time
,它将需要执行的动作作为参数,并测量和打印执行动作所需的时间。
代码:
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
我喜欢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";
}
}
- 1. 测量Java执行时间
- 2. 是否存在runonce命令来限制命令执行
- 3. 命令行命令的执行时间
- 4. redis SLOWLOG命令是否测量CPU时间或实时?
- 5. Java命令没有在appium中执行
- 6. 使用clock()来测量执行时间
- 7. SPARQL查询执行时间测量命令
- 8. 在运行时执行java命令
- 9. 在linux中测量执行时间
- 10. 测量java短时间运行的线程执行时间
- 11. 使用RDTSC指令在C中测量代码执行时间
- 12. 测量时间执行
- 13. C++测量执行时间
- 14. 测量执行时间
- 15. 测量执行时间短
- 16. 使用Java来执行WIndows命令行命令
- 17. 无法执行来自java的命令
- 18. 执行来自Java的CLI命令
- 19. 无法在C(popen)中执行命令执行时间?
- 20. 在Java中执行Linux nodejs命令行
- 21. 在Java中执行shell命令行
- 22. 是否有命令执行未包装在框架中的硒测试?
- 23. Java:在Eclipse中运行java文件时执行cmd命令?
- 24. Java - 运行时命令执行
- 25. 是否有在Ruby中执行外部命令的DSL/gem?
- 26. mysql命令行返回执行时间?
- 27. 没有输出的java内执行的命令行bash命令是不是像
- 28. java命令行执行
- 29. R中测量功能执行时间
- 30. 在Java终端中执行Linux命令
我会在构造函数中设置'start',这使得它更难以错误地使用类;-) – 2011-03-29 11:38:07
好主意,我会更改代码。 – RoflcoptrException 2011-03-29 11:39:30
我认为不需要重置方法。很好的例子。 – 2011-03-29 12:49:14