StopWatch对应的中文名称为秒表,经常我们对一段代码耗时检测的代码如下:

long startTime = System.currentTimeMillis();

// 你的业务代码

long endTime = System.currentTimeMillis();

long costTime = endTime -startTime;

System.err.println("该段代码耗时:" + costTime + " ms");

改进的代码写法:

我们可以利用已有的工具类中的秒表,常见的秒表工具类有org.springframework.util.StopWatch、org.apache.commons.lang.time.StopWatch以及谷歌提供的guava中的秒表(这个我没怎么用过)。这里重点讲下,org.springframework.util.StopWatch的用法。

org.springframework.util.StopWatch的用法:

查看其源码:

public class StopWatch {

private final String id;

private boolean keepTaskList;

private final List<StopWatch.TaskInfo> taskList;

private long startTimeMillis;

private boolean running;

private String currentTaskName;

private StopWatch.TaskInfo lastTaskInfo;

private int taskCount;

private long totalTimeMillis;

...

}

可以看到有一个List<StopWatch.TaskInfo> taskList,用于存储一组任务的耗时时间。这个很有用比如:我们可以记录多段代码耗时时间,然后一次性打印(这里:org.springframework.util.StopWatch提供了一个prettyString()函数用于按照指定格式打印出耗时)

举个例子:

public static void main(String[] args) throws InterruptedException {

// 定义一个计数器

StopWatch stopWatch = new StopWatch("统一一组任务耗时");

// 统计任务一耗时

stopWatch.start("任务一");

Thread.sleep(1000);

stopWatch.stop();

// 统计任务二耗时

stopWatch.start("任务二");

Thread.sleep(2000);

stopWatch.stop();

// 打印出耗时

String result = stopWatch.prettyPrint();

System.err.println(result);

}

结果为:

StopWatch '统一一组任务耗时': running time (millis) = 3000

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

ms % Task name

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

01000 033% 任务一

02000 067% 任务二

分析:

可以看到可以统一定义一个计数器为“统一一组任务耗时”,然后看到整段代码的耗时。以及各个部分程序代码的执行时间,并且输出的格式帮我们整理好了。

Spring框架中stopwatch(秒表)的更多相关文章

  1. 再析在spring框架中解决多数据源的问题

    在前面我写了<如何在spring框架中解决多数据源的问题>,通过设计模式中的Decorator模式在spring框架中解决多数据源的问题,得到了许多网友的关注.在与网友探讨该问题的过程中, ...

  2. Spring框架中 配置c3p0连接池 完成对数据库的访问

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  3. Spring框架中ModelAndView、Model、ModelMap区别

    原文地址:http://www.cnblogs.com/google4y/p/3421017.html SPRING框架中ModelAndView.Model.ModelMap区别   注意:如果方法 ...

  4. Spring框架中的定时器 使用和配置

    Spring框架中的定时器 如何使用和配置 转载自:<Spring框架中的定时器 如何使用和配置>https://www.cnblogs.com/longqingyang/p/554543 ...

  5. 细说shiro之五:在spring框架中集成shiro

    官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...

  6. 【Spring】8、Spring框架中的单例Beans是线程安全的么

    看到这样一个问题:spring框架中的单例Beans是线程安全的么? Spring框架并没有对单例bean进行任何多线程的封装处理.关于单例bean的线程安全和并发问题需要开发者自行去搞定.但实际上, ...

  7. Spring5源码解析-Spring框架中的单例和原型bean

    Spring5源码解析-Spring框架中的单例和原型bean 最近一直有问我单例和原型bean的一些原理性问题,这里就开一篇来说说的 通过Spring中的依赖注入极大方便了我们的开发.在xml通过& ...

  8. Spring框架中IoC(控制反转)的原理(转)

    原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...

  9. spring框架中的@Import注解

    spring框架中的@Import注解 Spring框架中的@Import注解 在之前的文章中,作者介绍了Spring JavaConfig. 这是除了使用传统的XML文件之外,spring带来的新的 ...

随机推荐

  1. web安全-密码安全

    web安全-密码安全 1.密码的作用 2.储存 3.传输 4.替代方案 5.生物特征密码 (指纹 人脸) 6.密码单向变换彩虹表 组合 密码变换次数越多越安全 加密成本几乎不变(生成密码速度慢一点) ...

  2. mysql远程访问被禁止

    远程连接Mysql服务器的数据库,错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx  is not allowed to connect to this MySQL ...

  3. C语言数据结构-链式栈的实现-初始化、销毁、长度、取栈顶元素、查找、入栈、出栈、显示操作

    1.数据结构-链式栈的实现-C语言 //链式栈的链式结构 typedef struct StackNode { int data; struct StackNode *next; } StackNod ...

  4. Bootstrap中的Glyphicon 字体图标

    在Bootstrap框架中也为大家提供了近200个不同的icon图片,而这些图标都是使用CSS3的@font-face属性配合字体来实现的icon效果. 1 <!DOCTYPE html> ...

  5. php 常见递归实例

    //计算数组{1,1,2,3,5,8.......} 第n位值 function Process1($i){ if ($i == 0) return 0; if ($i == 1) return 1; ...

  6. Python的安装位置与Python库

    如何查看Python的安装位置: 输入 where python ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  7. vs如何在Windows身份验证下调试Web项目

    vs做的Web项目发布到IIS站点后,通常我们还希望利用vs来调试代码.如果Web在IIS中设置成了Windows身份验证,那么我们如何在vs调试的时候,也同样采用Windows身份认证进行调试呢? ...

  8. P2421 [NOI2002]荒岛野人

    传送门 答案不大于 $10^6$,考虑枚举答案 对于枚举的 ans,必须满足对于任意 i,j(i≠j) 都有 使式子$c_i+kp_i \equiv c_j+kp_j\ (mod\ ans)$成立的最 ...

  9. Codeforces 237E

    没啥好说的 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring& ...

  10. LeeCode(5. Longest Palindromic Substring)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...