Stopwatch:提供一组方法和属性,可以准确的测量运行时间。使用的时候需要引用命名空间:System.Diagnostics。 二、Stopwatch的简单使用 //创建Stopwatch实例Stopwatch sw =newStopwatch();//开始计时sw.Start();for(inti =0; i <100; i++) { Console.WriteLine(i); }//停止计时sw.Stop(); Console.WriteLine(...
importorg.springframework.util.StopWatch;publicclassStopWatchTest{publicstaticvoidmain(String[]args)throwsInterruptedException{StopWatchstopWatch=newStopWatch("1");stopWatch.start("task1");Thread.sleep(1000);stopWatch.stop();stopWatch.start("task2");Thread.sleep(2000);stopWatch.stop();stopWatch.st...
我们可以通过调用stopWatch.start(taskName)来启动一个名为taskName的任务,并在任务完成后调用stopWatch.stop(taskName)来停止计时。这样,StopWatch就会分别记录每个任务的时间。 获取计时数据 StopWatch提供了多种方法来获取计时数据,包括获取总时间、各个任务的时间和百分比等。我们可以通过stopWatch.getTotalTimeMillis(...
Stopwatch stopwatch =newStopwatch(); 启动计时器: stopwatch.Start(); 执行要测量时间的代码块或程序。 停止计时器: stopwatch.Stop(); 获取经过的时间: TimeSpanelapsedTime = stopwatch.Elapsed; 可选:重置计时器: stopwatch.Reset(); 完整的示例代码如下所示: usingSystem;usingSystem.Diagnostics;publiccla...
import org.springframework.util.StopWatch;publicclassOTest{publicstaticvoidmain(String[] args) throws InterruptedException{ StopWatch stopWatch =newStopWatch();// 任务一模拟休眠3秒钟stopWatch.start("TaskOneName"); Thread.sleep(1000*3); System.out.println("当前任务名称:"+ stopWatch.currentTaskName...
1、new StopWatch() /*** org.springframework.util.StopWatch ***/ public StopWatch() { this(""); } private final String id;// 唯一标识 public StopWatch(String id) { this.id = id; } 1. 2. 3. 4. 5. 6. 7. 8. 9.
stopWatch.stop(); System.out.println(stopWatch.prettyPrint()); } } 单个计时看起来差别不大,但如果在单个线程我们要多次计算执行时间,那么StopWatch工具就更方便了,如下例子: public static void main1(String[] args) { StopWatch stopWatch = new StopWatch(); ...
public class StopWatchTest { public static void main(String[] args) throws InterruptedException { StopWatch stopWatch = new StopWatch(“测试秒表”); stopWatch.start(“暂停100毫秒”); Thread.sleep(100 * 1); stopWatch.stop(); stopWatch.start(“暂停200毫秒”); ...
StopWatch是Spring框架提供的一个简单而强大的计时工具类,用于精确测量代码执行时间。它可以帮助开发者快速分析程序的性能瓶颈,从而优化代码,提高程序运行效率。StopWatch基于纳秒级别的时间计算,支持多个任务的计时,并且可以方便地输出计时结果。 二、StopWatch基本功能 开始/停止计时:StopWatch提供了开始和停止计时的方法,...
使用StopWatch的基本步骤如下: 创建StopWatch实例 首先,我们需要创建一个StopWatch实例。这可以通过调用StopWatch的构造函数来实现,例如: StopWatch stopWatch = new StopWatch(); 开始计时 在需要测量时间的代码块之前,调用StopWatch的Start方法开始计时。例如: stopWatch.start(“task1”); 这里,“task1”是一个任...