在学习spring的时候,看到关于统计时间的类,比较好奇,就记录下来,以便以后用到可以直接使用
org.springframework.util.StopWatch

StopWatch该类在统计时间的时候,必须得前一个对象关闭才能创建新的StopWatch,并且在统计完成后,只需要将其输出,就可以像报表一样,显示统计的时间

在开发中,常用于统计时间的是 使用 System.currentTimeMillis();进行统计,并且当执行完毕后,

还需要相减,才能得到最终时间值,不过,stopWatch差不多也是类似功能吧

	public static void main(String[] args) throws InterruptedException {
StopWatch sw = new StopWatch();
sw.start("读取文件");
Thread.sleep(1000);
sw.stop();
sw.start("文件删除");
Thread.sleep(100);
sw.stop();
sw.start("文件拷贝");
Thread.sleep(10);
sw.stop();
System.out.println(sw.prettyPrint()); long stime =System.currentTimeMillis();
Thread.sleep(1000);
long etime =System.currentTimeMillis();
System.out.println("执行时间:"+(etime-stime));
}

执行结果:
StopWatch '': running time (millis) = 1110

-----------------------------------------

ms % Task name

-----------------------------------------

01000 090% 读取文件

00100 009% 文件删除

00010 001% 文件拷贝

执行时间:1000

查看其内部源码,我们可以看到,该类封装了System.currentTimeMillis();在同一个stopWatch对象中,将每次stop都存放在linklist中,在统计的时候,再取出输出

	private final List<TaskInfo> taskList = new LinkedList<TaskInfo>();

	public void start(String taskName) throws IllegalStateException {
if (this.running) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
}
this.startTimeMillis = System.currentTimeMillis();
this.running = true;
this.currentTaskName = taskName;
} /**
* Stop the current task. The results are undefined if timing
* methods are called without invoking at least one pair
* {@link #start()} / {@link #stop()} methods.
* @see #start()
*/
public void stop() throws IllegalStateException {
if (!this.running) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
}
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(lastTaskInfo);
}
++this.taskCount;
this.running = false;
this.currentTaskName = null;
} /**
* Return an array of the data for tasks performed.
*/
public TaskInfo[] getTaskInfo() {
if (!this.keepTaskList) {
throw new UnsupportedOperationException("Task info is not being kept!");
}
return this.taskList.toArray(new TaskInfo[this.taskList.size()]);
}

StopWatch的用法的更多相关文章

  1. Spring框架中stopwatch(秒表)

    StopWatch对应的中文名称为秒表,经常我们对一段代码耗时检测的代码如下: long startTime = System.currentTimeMillis(); // 你的业务代码 long ...

  2. 计时任务之StopWatch

    StopWatch对应的中文名称为秒表,经常我们对一段代码耗时检测的代码如下: long startTime = System.currentTimeMillis(); // 业务处理代码 doSom ...

  3. openTK学习

    简介 the Open Tool Kit (OpenTK), 是对 OpenGL.OpenAL.OpenCL 的跨平台的封装,使用 C# 编写,它可以用在Mono.dotNet的语言:c#.VB.C+ ...

  4. iMacros 入门教程-基础函数介绍(4)

    imacros的TRAY函数用法 这个函数的功能就是隐藏或显示,当执行imacros文件的时候,出现在特定标签的imacros图标 TRAY HIDE 就是隐藏图标 TRAY SHOW 就是显示图标 ...

  5. 监控代码运行时长 -- StopWatch用法例程

    在.net环境下,精确的测量出某段代码运行的时长,在网络通信.串口通信以及异步操作中很有意义.现在做了简单的总结.具体代码如下: (1).首先 using System.Diagnostics; (2 ...

  6. Spring 中StopWatch用法

    背景 有时我们在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,然后这样如果执行大量测试的话就很麻烦,并且不直观,如果想对执行的时间做进 ...

  7. spring StopWatch用法

    背景 有时我们在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,然后这样如果执行大量测试的话就很麻烦,并且不直观,如果想对执行的时间做进 ...

  8. spring计时工具类stopwatch用法

    public class Test { public static void main(String[] args) { StopWatch stopWatch = new StopWatch(); ...

  9. stopWatch 用法

    package com.example.stopwatch; import org.springframework.util.StopWatch; public class TestStopWatch ...

随机推荐

  1. iOS: AFNetworking手动配置(iOS7.1, AF2.2.4)

    一.下载AFNetworking. 二.将AFNetworking-master下的AFNetworking目录拖入到项目中 三.为项目添加Linking to a Library or Framew ...

  2. 练习2 H题 - 求数列的和

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 数列的 ...

  3. WCF SOAP

    WCF SOAP服务端解析 ASP.NET Core中间件(Middleware)进阶学习实现SOAP 解析. 本篇将介绍实现ASP.NET Core SOAP服务端解析,而不是ASP.NET Cor ...

  4. 关于android app两次点击返回键退出的处理

    现在的android app在开发时,引入了两次点击返回键退出app的设计 为了避免用户误触,这个设计很人性化 中文网上社区有些同学贴了一些实现的例子,我觉得不是很好 代码如下 public bool ...

  5. 使用Unity游戏引擎在IOS模拟器中运行的方法

    在Unity编译IOS程序时,在Unity导航栏菜单中选择Edit->ProjectSettings ->Player(菜单项)选择IOS平台在下方SDK Version处选择运行设备为I ...

  6. h.264宏块与子宏块类型

    宏块类型mb_type 宏块类型表示的是宏块不同的分割和编码方式,在h.264的语法结构中,宏块类型在宏块层(macroblock_layer)中用mb_type表示(请参考h.264语法结构分析中的 ...

  7. 5种php加密工具zendGuard、ionCube、SourceCop、SourceGuardian、phpShield

    PHP做桌面应用的想法: 除去icudt55.dll,PHP7用7ZIP压缩后不足7MB,而PHP自带了SQLite和CLI HTTP Server,用户打开浏览器就能访问PHP开发的桌面应用.如果源 ...

  8. 在非UI线程中更改UI(Delphi使用隐藏窗口来处理,QT使用信号槽)

    在Delphi里我记得是使用TThread.Synchronize(TThreadMethod),原理是利用了一个隐藏窗口来处理. 在QT Debug模式一下,碰到了同样的问题,显示错误: canno ...

  9. 添加Fragment注意事项

    配置(Configuration )改变是Android应用生命周期的一部分,如果发生了该事件(屏幕从横屏换行为竖屏),就会导致Activity被销毁然后重新创建.就算您在配置文件中设定Activit ...

  10. VMware Workstation 不可恢复错误: (vcpu-0)