stopwatch用法
时间: 2023-08-21 13:13:12 浏览: 50
Stopwatch是一个计时器工具类,可以用来计算程序部分代码的执行时间。使用Stopwatch可以更方便地计算耗时,而不需要使用System.currentTimeMillis()来手动计算。以下是Stopwatch的用法示例:
1. 创建Stopwatch实例:
StopWatch stopWatch = new StopWatch("测试");
2. 开始计时:
stopWatch.start("任务1");
3. 执行任务代码:
// 执行任务1的代码
4. 停止计时:
stopWatch.stop();
5. 获取任务耗时:
StopWatch.TaskInfo\[\] taskInfo = stopWatch.getTaskInfo();
for (StopWatch.TaskInfo info : taskInfo) {
System.out.println(info.getTimeMillis());
}
6. 获取总体耗时:
long totalTime = stopWatch.getTotalTimeMillis();
7. 获取上一个任务的耗时:
long lastTaskTime = stopWatch.getLastTaskTimeMillis();
8. 格式化输出计时结果:
String formattedTime = stopWatch.prettyPrint();
9. 检查计时器是否在运行中:
boolean isRunning = stopWatch.isRunning();
请注意,Stopwatch实例一次只能开启一个任务,不能同时start多个任务。必须在一个任务stop之后才能开启新的任务。如果需要同时开启多个任务,需要创建不同的Stopwatch实例。
以上是Stopwatch的基本用法,你可以根据需要进行相应的调整和扩展。
#### 引用[.reference_title]
- *1* [Stopwatch用法](https://blog.csdn.net/qq_38261544/article/details/120333259)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [springboot学习(七十四) spring中时钟计时器StopWatch的使用](https://blog.csdn.net/u011943534/article/details/128219115)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]