org.springframework.util.StopWatch使用说明

StopWatch是一个用于测量代码执行时间和性能的工具类。通过创建对象,启动和停止计时,可以获取代码块的执行时间。它支持间隔计时,保留任务清单,以及打印格式化的执行时间报告。此外,StopWatch还提供重置功能,以便重复使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

StopWatch是一个简单实用的秒表工具类,可以用来评估代码块的执行时间和性能。
以下是StopWatch的主要用法:

  1. 创建StopWatch对象
StopWatch stopWatch = new StopWatch();
StopWatch stopWatch2 = new StopWatch("读取商品信息");
  1. 启动秒表计时
stopWatch.start(); 
  1. 执行需要计时的代码块
  2. 停止秒表计时
stopWatch.stop(); 
  1. 获取执行时间
//这将获取从start()到stop()的总执行时间,以毫秒为单位。
long time = stopWatch.getTotalTimeMillis();
  1. 间隔计时
    可以通过多次调用start()和stop()进行间隔计时。
    例如:
StopWatch stopWatch = new StopWatch();
stopWatch.setKeepTaskList(true);

stopWatch.start("任务1");
doSomething();
stopWatch.stop();

stopWatch.start("任务2");
doSomethingElse();
stopWatch.stop();

long task1Time = stopWatch.getTaskTimeMillis("任务1");
long task2Time = stopWatch.getTaskTimeMillis("任务2");
System.out.println("任务1时间: " + task1Time + "ms");
System.out.println("任务2时间: " + task2Time + "ms");

然后可以通过getTaskTimeMillis(String taskName)获取指定任务的执行时间。
这需要先调用setKeepTaskList(true)保留任务清单。

  1. 打印友好的报告
    可以调用prettyPrint()方法打印一个格式友好的时间报告,如:

ms % Task name

1000 50% 任务1
1000 50% 任务2
2000 100%

这需要先进行间隔计时,并设置setKeepTaskList(true)。
它会打印:

  • 每个任务的执行时间(毫秒ms)
  • 占总时间的百分比%
  • 任务名称
  • 总共的执行时间
  1. 重置秒表
    可以调用reset()方法重置秒表,以再次从零开始进行计时。
总结

StopWatch的主要用法是:

  1. 创建StopWatch对象
  2. 调用start()开始计时
  3. 执行代码并获取时间
  4. 调用stop()停止计时
  5. 获取总时间或任务时间
  6. 调用prettyPrint()打印友好报告
  7. 调用reset()重置秒表

它提供了几个有用的方法:

  • start(): 开始秒表计时
  • stop(): 停止秒表计时
  • getTotalTimeMillis(): 获取从start到现在的总执行时间(毫秒)
  • getLastTaskTimeMillis(): 获取上次start到stop的计时时间(毫秒),可以用于间隔计时
  • setKeepTaskList(boolean): 设置是否保留任务清单,如果为true可以获取多个start-stop间隔时间
  • getTaskCount(): 获取start-stop间隔的数量
  • stopWatch.prettyPrint()方法会将StopWatch记录的计时信息以友好的格式打印出来。
  • getTaskInfo(TaskInfo taskInfo): 获取指定间隔的计时信息
  • reset()方法重置秒表,以再次从零开始进行计时。
2025-07-11 09:54:15.937 ERROR 13820 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.data.repository.config.RepositoryConfigurationDelegate.registerRepositoriesIn(RepositoryConfigurationDelegate.java:210) The following method did not exist: 'org.springframework.util.StopWatch$TaskInfo org.springframework.util.StopWatch.lastTaskInfo()' The calling method's class, org.springframework.data.repository.config.RepositoryConfigurationDelegate, was loaded from the following location: jar:file:/C:/Users/20651/.m2/repository/org/springframework/data/spring-data-commons/3.2.0/spring-data-commons-3.2.0.jar!/org/springframework/data/repository/config/RepositoryConfigurationDelegate.class The called method's class, org.springframework.util.StopWatch, is available from the following locations: jar:file:/C:/Users/20651/.m2/repository/org/springframework/spring-core/5.3.33/spring-core-5.3.33.jar!/org/springframework/util/StopWatch.class The called method's class hierarchy was loaded from the following locations: org.springframework.util.StopWatch: file:/C:/Users/20651/.m2/repository/org/springframework/spring-core/5.3.33/spring-core-5.3.33.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.data.repository.config.RepositoryConfigurationDelegate and org.springframework.util.StopWatch 进程已结束,退出代码为 1
最新发布
07-12
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘彦青-Yannis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值