推荐:
一、简单计时
public class Test {
public static void main(String[] args) throws InterruptedException {
getTime();
}
public static void getTime() throws InterruptedException {
long start = System.currentTimeMillis();
Thread.sleep(100);
long end = System.currentTimeMillis();
long start2 = System.currentTimeMillis();
Thread.sleep(200);
long end2 = System.currentTimeMillis();
System.out.println("任务一:" + (end - start));
System.out.println("任务二:" + (end2 - start2));
}
}
二、StopWatch计时
当前使用的SotpWatch
- 包:spring-core-5.25.RELEASE.jar
- 类:org.springframework.util.StopWatch
- start():开始记录,可自定义任务名称
- stop():停止记录
- prettyPrint():带格式自动输出各个任务的情况(执行耗时,以及执行时间百分比)
- getTotalTimeMillis():总用时,单位ms
1、pom依赖
引入 Spring 核心包,Spring MVC 和 Spring Boot 都已经自动引入了该包
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
2、JAVA代码
import org.springframework.util.StopWatch;
public class Test {
public static void main(String[] args) throws InterruptedException {
getTimeByStopWatch();
}
public static void getTimeByStopWatch() throws InterruptedException {
StopWatch stopWatch = new StopWatch("多个任务用时");
stopWatch.start("任务一");
Thread.sleep(100);
stopWatch.stop();
stopWatch.start("任务二");
Thread.sleep(200);
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
System.out.println("多个任务总用时:"+stopWatch.getTotalTimeMillis()+"毫秒");
}
}
3、源码分析
使用 LinkedList 实现了一个叫做 taskList 的数组队列
当任务开始时,使用System.currentTimeMillis()
方法来获取时间
当任务结束时,计算当前任务耗时
,并构建一个描述当前任务的TaskInfo 对象
,并把它放入taskList 数组队列
。
public void start(String taskName) throws IllegalStateException {
if (this.currentTaskName != null) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
}
this.currentTaskName = taskName;
this.startTimeNanos = System.nanoTime();
}
public void stop() throws IllegalStateException {
if (this.currentTaskName == null) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
}
long lastTime = System.nanoTime() - this.startTimeNanos;
this.totalTimeNanos += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(this.lastTaskInfo);
}
++this.taskCount;
this.currentTaskName = null;
}
当任务打印时,执行
prettyPrint()
方法,从taskList数组队列中依次取出任务,然后进行格式化输出
public String prettyPrint() {
StringBuilder sb = new StringBuilder(shortSummary());
sb.append('\n');
if (!this.keepTaskList) {
sb.append("No task info kept");
}
else {
sb.append("---------------------------------------------\n");
sb.append("ns % Task name\n");
sb.append("---------------------------------------------\n");
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumIntegerDigits(9);
nf.setGroupingUsed(false);
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMinimumIntegerDigits(3);
pf.setGroupingUsed(false);
for (TaskInfo task : getTaskInfo()) {
sb.append(nf.format(task.getTimeNanos())).append(" ");
sb.append(pf.format((double) task.getTimeNanos() / getTotalTimeNanos())).append(" ");
sb.append(task.getTaskName()).append("\n");
}
}
return sb.toString();
}