StopWatch的用法
在学习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的用法的更多相关文章
- Spring框架中stopwatch(秒表)
StopWatch对应的中文名称为秒表,经常我们对一段代码耗时检测的代码如下: long startTime = System.currentTimeMillis(); // 你的业务代码 long ...
- 计时任务之StopWatch
StopWatch对应的中文名称为秒表,经常我们对一段代码耗时检测的代码如下: long startTime = System.currentTimeMillis(); // 业务处理代码 doSom ...
- openTK学习
简介 the Open Tool Kit (OpenTK), 是对 OpenGL.OpenAL.OpenCL 的跨平台的封装,使用 C# 编写,它可以用在Mono.dotNet的语言:c#.VB.C+ ...
- iMacros 入门教程-基础函数介绍(4)
imacros的TRAY函数用法 这个函数的功能就是隐藏或显示,当执行imacros文件的时候,出现在特定标签的imacros图标 TRAY HIDE 就是隐藏图标 TRAY SHOW 就是显示图标 ...
- 监控代码运行时长 -- StopWatch用法例程
在.net环境下,精确的测量出某段代码运行的时长,在网络通信.串口通信以及异步操作中很有意义.现在做了简单的总结.具体代码如下: (1).首先 using System.Diagnostics; (2 ...
- Spring 中StopWatch用法
背景 有时我们在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,然后这样如果执行大量测试的话就很麻烦,并且不直观,如果想对执行的时间做进 ...
- spring StopWatch用法
背景 有时我们在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,然后这样如果执行大量测试的话就很麻烦,并且不直观,如果想对执行的时间做进 ...
- spring计时工具类stopwatch用法
public class Test { public static void main(String[] args) { StopWatch stopWatch = new StopWatch(); ...
- stopWatch 用法
package com.example.stopwatch; import org.springframework.util.StopWatch; public class TestStopWatch ...
随机推荐
- 学习总结之Log4NET
通过在网上查找了一些资料,用了些时间学习了log4NET,做了一个小小的总结,说一下它的特点吧 首先呢log4NET是.Net下一个非常优秀的开源日志记录组件.它可以将日志分成不同等级,也可以按照我们 ...
- [译]36 Days of Web Testing(四)
Day 19: UX 用户体验 Why ? 最近UX变得越来越火,用户提现往往会直接联想到易用性和设计. 在我看来,UX不仅仅是这两点.UX, User Experience ,对我而言,不单单是产 ...
- java 上下文切换
上下文概念 在高性能编程时,经常接触到多线程. 起初我们的理解是, 多个线程并行地执行总比单个线程要快, 就像多个人一起干活总比一个人干要快. 然而实际情况是, 多线程之间需要竞争IO设备, 或者竞争 ...
- IndexedDB
http://www.tfan.org/indexeddb/ http://fnvfox.appspot.com/thankyou.html http://www.tfan.org/wechat-on ...
- Jamie's Contact Groups
poj2289:http://poj.org/problem?id=2289 题意:给定一个规模为n的名单,要将名单中的人归到m个组中,给出每个人可能的分组号,需要确定一种分配方案,是的最大规模的组最 ...
- 手动更改WIN远程桌面端口,要改两个地方的注册表哟
看到我的服务器有老多人在用桌面连接,虽然进不去,但他们不停地试,浪费掉不少服务器资源,我看到网上有不少关于修改3389的介绍.修改3389的工具,一些工具一点用都没有,纯属扯淡.修改后照样是3389. ...
- Ruby安装和简介
Ruby下载地址:https://www.ruby-lang.org/zh_cn/downloads/ 我安装的是RubyInstaller.it is a self-contained Window ...
- 【HDOJ】1394 Minimum Inversion Number
逆序数的性质.1. 暴力解 #include <stdio.h> #define MAXNUM 5005 int a[MAXNUM]; int main() { int n; int i, ...
- Spark(Hive) SQL数据类型使用详解(Python)
Spark SQL使用时需要有若干“表”的存在,这些“表”可以来自于Hive,也可以来自“临时表”.如果“表”来自于Hive,它的模式(列名.列类型等)在创建时已经确定,一般情况下我们直接通过Spar ...
- php 中 global 与 $GLOBAL 由引用产生的区别
很多人都认为global和$GLOBALS[]只是写法上面的差别,其实不然. 根据官方的解释是 $GLOBALS['var'] 是外部的全局变量$var本身. global $var 是外部$var的 ...