在学习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. C#获取硬盘空间信息

    /// <summary> /// 获取指定驱动器的空间总大小(单位为B) /// </summary> /// <param name="str_HardDi ...

  2. JsRender

    JsRender 不少前端人员应该都用过,它是一个比较强大的模板,不牵涉太多技术依赖,使用起来非常舒服.我本人在前端开发中使用React之前,都是用的它了(实际上我感觉React没有JsViewes好 ...

  3. 移动端meta标签整理-备

    分类 在介绍移动端特有 meta 标签之前,先简单说一下 HTML meta 标签的一些知识. meta 标签包含了 HTTP 标题信息 (http-equiv) 和 页面描述信息 (name). h ...

  4. Python学习笔记(一)Python安装及环境变量的配置

    1.下载python安装包. 下载地址:https://www.python.org/ 2.配置环境变量 找到python的安装路径.C:\Python27;script的路径:C:\Python27 ...

  5. LightOj_1284 Lights inside 3D Grid

    题目链接 题意: 给一个X * Y * Z 的立方体, 每个单位立方体内都有一盏灯, 初始状态是灭的, 你每次操作如下: 1)选择一个点(x1, y1, z1)     再选择一个点(x2, y2, ...

  6. python学习随笔

    1 高阶函数的使用: import math def add(x, y, f): return f(x) + f(y) sq = math.sqrt print add(25, 9,sq) 2. ma ...

  7. [BZOJ 1058] [ZJOI2007] 报表统计 【平衡树】

    题目链接:BZOJ - 1058 题目分析 这道题看似是需要在序列中插入一些数字,但其实询问的内容只与相邻的元素有关. 那么我们只要对每个位置维护两个数 Ai, Bi, Ai 就是初始序列中 i 这个 ...

  8. MATLAB中的结构数组

    MATLAB中的结构数组 结构数组: 结构是包含一组记录的数据类型,而记录则是存储在相应的字段中.结构的字段可以是任意一种MATLAB数据类型的变量或者对象.结构类型的变量也可以是一维的.二维的或多维 ...

  9. libstdc++.so.5: cannot open shared object file: No such file or directory

    中文分词一般会选择ICTCLAS的模块,虽然不能说很完美,但也算是一个不错的选择.它提供了windows版本和linux版本,并支持C/C#/JNI接口.这本来是一个不错的事情,但版本一多,官方似乎就 ...

  10. Java语言基础(四) String和StringBuffer的区别

    Java提供了两个字符串类:String和StringBuffer. String提供了数值不可变的字符串,而StringBuffer提供的字符串对象可以进行修改. 当知道字符数据要改变的时候就可以使 ...