- 如何获取方法的执行时间?
- 是否有一个
Timer
实用程序类来计算任务所需的时间等?
大多数在Google上搜索的结果都是关于调度线程和任务的定时器,这不是我想要的。
Timer
实用程序类来计算任务所需的时间等?大多数在Google上搜索的结果都是关于调度线程和任务的定时器,这不是我想要的。
new Timer(""){{
// code to time
}}.timeMe();
public class Timer {
private final String timerName;
private long started;
public Timer(String timerName) {
this.timerName = timerName;
this.started = System.currentTimeMillis();
}
public void timeMe() {
System.out.println(
String.format("Execution of '%s' takes %dms.",
timerName,
started-System.currentTimeMillis()));
}
}
我基本上做的就是这个,但考虑到热点编译的工作原理,如果你想得到准确的结果,你需要放弃前面的几次测量,并确保你正在使用的方法在一个真实的世界(阅读应用特定)中。
如果JIT决定编译它,你的数字将会有很大变化。所以请注意。
有几种方法可以做到这一点。我通常会回退到使用类似于以下内容的东西:
long start = System.currentTimeMillis();
// ... do something ...
long end = System.currentTimeMillis();
或者使用System.nanoTime()来完成相同的事情。
对于更多基准测试方面的内容,似乎还有这个:http://jetm.void.fm/ 不过我从未尝试过。
如果您需要墙钟时间
long start_time = System.currentTimeMillis();
object.method();
long end_time = System.currentTimeMillis();
long execution_time = end_time - start_time;
long startTime = System.currentTimeMillis();
// code goes here
long finishTime = System.currentTimeMillis();
long elapsedTime = finishTime - startTime; // elapsed time in milliseconds
Instant
的新类。根据文档:
Instant表示时间线上一纳秒的开始。此类用于生成时间戳以表示机器时间。即使需要存储比long更大的数字,瞬时的范围也无法承受。为了实现这一点,该类存储表示纪元秒和表示秒内纳秒的int的long,后者始终在0到999,999,999之间。从标准Java时代1970-01-01T00:00:00Z开始测量纪元秒,纪元之后的瞬时值为正值,而较早的瞬时值为负值。对于纪元秒和纳秒部分,较大的值始终比较小的值更靠近时间线。
可以使用以下方法:
Instant start = Instant.now();
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Instant end = Instant.now();
System.out.println(Duration.between(start, end));
它会打印出PT7.001S
。
好的,这是一个简单的类,用于对您的函数进行简单的定时。下面有一个示例。
public class Stopwatch {
static long startTime;
static long splitTime;
static long endTime;
public Stopwatch() {
start();
}
public void start() {
startTime = System.currentTimeMillis();
splitTime = System.currentTimeMillis();
endTime = System.currentTimeMillis();
}
public void split() {
split("");
}
public void split(String tag) {
endTime = System.currentTimeMillis();
System.out.println("Split time for [" + tag + "]: " + (endTime - splitTime) + " ms");
splitTime = endTime;
}
public void end() {
end("");
}
public void end(String tag) {
endTime = System.currentTimeMillis();
System.out.println("Final time for [" + tag + "]: " + (endTime - startTime) + " ms");
}
}
使用示例:
public static Schedule getSchedule(Activity activity_context) {
String scheduleJson = null;
Schedule schedule = null;
/*->*/ Stopwatch stopwatch = new Stopwatch();
InputStream scheduleJsonInputStream = activity_context.getResources().openRawResource(R.raw.skating_times);
/*->*/ stopwatch.split("open raw resource");
scheduleJson = FileToString.convertStreamToString(scheduleJsonInputStream);
/*->*/ stopwatch.split("file to string");
schedule = new Gson().fromJson(scheduleJson, Schedule.class);
/*->*/ stopwatch.split("parse Json");
/*->*/ stopwatch.end("Method getSchedule");
return schedule;
}
控制台输出示例:
Split time for [file to string]: 672 ms
Split time for [parse Json]: 893 ms
Final time for [get Schedule]: 1565 ms
我的机器性能测试结果
System.nanoTime() : 750纳秒
System.currentTimeMillis() : 18纳秒
如前所述,System.nanoTime()
被认为是用来测量经过的时间。但要注意在循环等内部使用时的成本。
StopWatch stopWatch = new StopWatch()
stopWatch.start(); //start stopwatch
// write your function or line of code.
stopWatch.stop(); //stop stopwatch
stopWatch.getTotalTimeMillis() ; ///get total time
Stopwatch文档: 简单的秒表,可以计时多个任务,显示总运行时间和命名任务的运行时间。隐藏了System.currentTimeMillis()的使用,提高了应用程序代码的可读性并降低了计算错误的可能性。 请注意,该对象不是为线程安全而设计的,也不使用同步。该类通常用于在概念验证和开发过程中验证性能,而不是作为生产应用程序的一部分。
Instant
类:https://dev59.com/InVC5IYBdhLWcg3wz0h9#30975902 - akhil_mittal