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 ...
随机推荐
- 如何在win7上面安装python的包
最近在win7上面搞python,然后写的一些代码涉及到了对Excel的读写.所以需要用到包xlrd xlwt xlutils. 但问题是这些包import后显示的是找不到.错误提示是:Import ...
- 2016021903 - 下载安装使用Memory Analyzer
Memory Analyzer是做什么的? 分析java程序中分析内存泄露问题. 1.下载Memory Analyzer Memory Analyzer下载地址:http://www.eclipse. ...
- Login过滤器
继承自ActionFilterAttibute public override void OnActionExecuting(ActionExecutingContext filterContext) ...
- System.Reflection.Assembly.GetEntryAssembly()获取的为当前已加载的程序集
今天在使用System.Reflection.Assembly.GetEntryAssembly()获取程序集时,发现获取的程序集不全.原来是因为C#的程序集为延迟加载,此方法只获取当前已加载的,未加 ...
- java高级:weakReference
Java WeakReference的理解与使用 http://www.tuicool.com/articles/imyueq
- Contest 20140923 潛行世界 拓撲排序,期望
潜行世界 查看 提交 统计 提问 总时间限制: 10000ms 内存限制: 256000kB 描述 HJA和学弟还在旅游中,这次他们来到了潜行世界.潜行世界是一个N个点M条边的有向无环图.每条路对 ...
- xcode5时代如何设置Architectures和Valid Architectures
目前ios的指令集有以下几种: 1,armv6,支持的机器iPhone,iPhone2,iPhone3G及对应的iTouch 2,armv7,支持的机器iPhone4,iPhone4S 3,armv7 ...
- iphone获取sim卡信息
/* iphone获取sim卡信息 1.加入一个Framework(CoreTelephony.framework). 2.引入头文件 #import <CoreTelephony/CTTele ...
- angularjs 资源集合
AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足. 源码托管在Github上,其通过使用指令(directives)结构来扩展HTML词汇 ...
- bootcamp
为了鄙社自主研发的html5studio和mist,我给Air划了32G装windows囧 第一要注意的是,必须使用bootcamp划分将要安装windows的分区,不要在windows安装过程中删除 ...