- 如何获取方法的执行时间?
- 是否有一个
Timer
实用程序类来计算任务所需的时间等?
大多数在Google上搜索的结果都是关于调度线程和任务的定时器,这不是我想要的。
Timer
实用程序类来计算任务所需的时间等?大多数在Google上搜索的结果都是关于调度线程和任务的定时器,这不是我想要的。
我修改了正确答案的代码以在几秒钟内获得结果:
long startTime = System.nanoTime();
methodCode ...
long endTime = System.nanoTime();
double duration = (double)(endTime - startTime) / (Math.pow(10, 9));
Log.v(TAG, "MethodName time (s) = " + duration);
如果只想要知道时间,可以尝试以下方法。
long startTime = System.currentTimeMillis();
//@ Method call
System.out.println("Total time [ms]: " + (System.currentTimeMillis() - startTime));
如果Java拥有更好的函数支持将会很不错,这样需要测量的操作就可以被包装成一个块:
measure {
// your operation here
}
public interface Timer {
void wrap();
}
public class Logger {
public static void logTime(Timer timer) {
long start = System.currentTimeMillis();
timer.wrap();
System.out.println("" + (System.currentTimeMillis() - start) + "ms");
}
public static void main(String a[]) {
Logger.logTime(new Timer() {
public void wrap() {
// Your method here
timeConsumingOperation();
}
});
}
public static void timeConsumingOperation() {
for (int i = 0; i<=10000; i++) {
System.out.println("i=" +i);
}
}
}
java.lang.Runnable
是一个@FunctionalInterface
,这意味着您可以将lambda表达式传递给任何以Runnable
作为参数的方法。您的timeThisCode(Runnable r)
可以简单地返回毫秒/纳秒或更详细的经过时间表示。 - Cornel Masson这里是格式化后的字符串,显示已经过去的时间,类似于谷歌搜索所需的时间:
long startTime = System.nanoTime();
// ... methodToTime();
long endTime = System.nanoTime();
long duration = (endTime - startTime);
long seconds = (duration / 1000) % 60;
// formatedSeconds = (0.xy seconds)
String formatedSeconds = String.format("(0.%d seconds)", seconds);
System.out.println("formatedSeconds = "+ formatedSeconds);
// i.e actual formatedSeconds = (0.52 seconds)
我实现了一个简单的计时器,我认为它非常有用:
public class Timer{
private static long start_time;
public static double tic(){
return start_time = System.nanoTime();
}
public static double toc(){
return (System.nanoTime()-start_time)/1000000000.0;
}
}
这样您就可以计时一个或多个操作:
Timer.tic();
// Code 1
System.out.println("Code 1 runtime: "+Timer.toc()+" seconds.");
// Code 2
System.out.println("(Code 1 + Code 2) runtime: "+Timer.toc()+"seconds");
Timer.tic();
// Code 3
System.out.println("Code 3 runtime: "+Timer.toc()+" seconds.");
Create a class with a method annotated with @AroundInvoke
;
@Singleton
public class TimedInterceptor implements Serializable {
@AroundInvoke
public Object logMethod(InvocationContext ic) throws Exception {
Date start = new Date();
Object result = ic.proceed();
Date end = new Date();
System.out.println("time: " + (end.getTime - start.getTime()));
return result;
}
}
Annotate the method that you want to monitoring:
@Interceptors(TimedInterceptor.class)
public void onMessage(final Message message) { ...
System.nanoTime()
是一个非常精确的系统工具,用于测量执行时间。但是要小心,如果您正在运行抢占式调度模式(默认情况下),此实用程序实际上会测量墙钟时间而不是 CPU 时间。因此,您可能会注意到不同的执行时间值从运行到运行,这取决于系统负载。如果您正在寻找 CPU 时间,我认为在实时模式下运行程序会有所帮助。您必须使用 RT Linux。链接:Linux 实时编程
public static <T> T timed (String description, Consumer<String> out, Supplier<T> code) {
final LocalDateTime start = LocalDateTime.now ();
T res = code.get ();
final long execTime = Duration.between (start, LocalDateTime.now ()).toMillis ();
out.accept (String.format ("%s: %d ms", description, execTime));
return res;
}
而调用代码可能是这样的:
public static void main (String[] args) throws InterruptedException {
timed ("Simple example", System.out::println, Timing::myCode);
}
public static Object myCode () {
try {
Thread.sleep (1500);
} catch (InterruptedException e) {
e.printStackTrace ();
}
return null;
}
还可以实现Timer
接口并在您的类的任何方法上执行
import java.util.function.*;
public interface Timer {
default void timeIt(Runnable r) {
timeIt(() -> { r.run(); return 0;});
}
default <S,T> T timeIt(Function<S,T> fun, S arg) {
long start = System.nanoTime();
T result = fun.apply(arg);
long stop = System.nanoTime();
System.out.println("Time: " + (stop-start)/1000000.0 + " msec");
return result;
}
default <T> T timeIt(Supplier<T> s) {
return timeIt(obj -> s.get(), null);
}
}
使用方法:
class MyClass implements Timer ..
timeIt(this::myFunction);
Instant
类:https://dev59.com/InVC5IYBdhLWcg3wz0h9#30975902 - akhil_mittal