bak*_*kal 227
您可以在之前和之后拍摄时间戳快照,然后重复几次实验以平均结果.还有可以为您执行此操作的分析器.
使用System.currentTimeMillis()
class TimeTest1 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
long total = 0;
for (int i = 0; i < 10000000; i++) {
total += i;
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println(elapsedTime);
}
}
Run Code Online (Sandbox Code Playgroud)
使用StopWatch类
你可以使用这个StopWatch
类,并调用start()
和stop
之前和方法之后.
class TimeTest2 {
public static void main(String[] args) {
Stopwatch timer = new Stopwatch().start();
long total = 0;
for (int i = 0; i < 10000000; i++) {
total += i;
}
timer.stop();
System.out.println(timer.getElapsedTime());
}
}
Run Code Online (Sandbox Code Playgroud)
看到这里.
应用性能应用
性能配置文件方法级CPU性能(执行时间).您可以选择分析整个应用程序或应用程序的一部分.
看到这里.
Vit*_*nko 223
更确切地说,我会使用nanoTime()
方法而不是currentTimeMillis()
:
long startTime = System.nanoTime();
myCall();
long stopTime = System.nanoTime();
System.out.println(stopTime - startTime);
Run Code Online (Sandbox Code Playgroud)
在Java 8中(输出格式为ISO-8601):
Instant start = Instant.now();
Thread.sleep(63553);
Instant end = Instant.now();
System.out.println(Duration.between(start, end)); // prints PT1M3.553S
Run Code Online (Sandbox Code Playgroud)
番石榴秒表:
Stopwatch stopwatch = Stopwatch.createStarted();
myCall();
stopwatch.stop(); // optional
System.out.println("Time elapsed: "+ stopwatch.elapsed(TimeUnit.MILLISECONDS));
Run Code Online (Sandbox Code Playgroud)
小智 41
检查一下:System.currentTimeMillis.
有了这个,您可以通过以下方式计算方法的时间:
long start = System.currentTimeMillis();
class.method();
long time = System.currentTimeMillis() - start;
Run Code Online (Sandbox Code Playgroud)
JJD*_*JJD 31
如果您为Android开发应用程序,您应该尝试TimingLogger
该类.
看看这些描述TimingLogger
helper类用法的文章:
如果您当前正在编写应用程序,那么答案是使用System.currentTimeMillis或System.nanoTime服务于上述人员指出的目的.
但是如果您已经编写了代码,并且您不想更改它,那么最好使用Spring的方法拦截器.例如,您的服务是:
public class MyService {
public void doSomething() {
for (int i = 1; i < 10000; i++) {
System.out.println("i=" + i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了避免更改服务,您可以编写自己的方法拦截器:
public class ServiceMethodInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = methodInvocation.proceed();
long duration = System.currentTimeMillis() - startTime;
Method method = methodInvocation.getMethod();
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
System.out.println("Method '" + methodName + "' took " + duration + " milliseconds to run");
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
还有可用于Java的开源API,例如BTrace.或者@bakkal和@Saikikos建议的Netbeans剖析器.谢谢.
如所建议的,nanoTime()在短时间尺度上非常精确。当需要这种精度时,您需要注意实际测量的内容。特别是不测量纳秒通话本身
long start1 = System.nanoTime();
// maybe add here a call to a return to remove call up time, too.
// Avoid optimization
long start2 = System.nanoTime();
myCall();
long stop = System.nanoTime();
long diff = stop - 2*start2 + start1;
System.out.println(diff + " ns");
Run Code Online (Sandbox Code Playgroud)
顺便说一下,由于