public class Test {

public static void main(String[] args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
}
}

打印每个任务执行时间,以及占总时间百分比  

package com.common.suanfa;  

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import org.springframework.util.StopWatch; public class Singleton {
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
Class name = Class.forName("com.common.suanfa.Singleton");
Method[] m = name.getDeclaredMethods();
StopWatch stopWatch = new StopWatch("test");
Object o = name.newInstance();
for (Method mm : m) {
if (mm.getName() != "main") {
stopWatch.start(mm.getName());
mm.invoke(o);
stopWatch.stop();
}
}
System.out.println(stopWatch.prettyPrint());
} private static void method1() {
int i = Integer.MAX_VALUE;
long sum = 0l;
while (i-- > ) {
sum += i;
}
System.out.println(sum);
} private static void method2() {
int i = Integer.MAX_VALUE;
long sum = 0l;
while ((i -= ) > ) {
sum += i;
}
System.out.println(sum);
}
}
output: StopWatch 'test': running time (millis) =
-----------------------------------------
ms % Task name
-----------------------------------------
% method1
% method2

spring计时工具类stopwatch用法的更多相关文章

  1. Spring的Assert工具类的用法

    简介 今天在看spring mvc源码时看到下面代码,感觉蛮有意思的,在这里记录下 Assert断言工具类,通常用于数据合法性检查,在JAVA编程中,通常会编写如下代码: if (name == nu ...

  2. Spring工具类ToStringBuilder用法简介

    比如说我们需要打印某个方法的User参数对象 package test; /** * * @author zhengtian * @time 2012-6-28 */ public class Use ...

  3. 2015第30周三Spring常用工具类

    文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...

  4. Spring常用工具类

    Spring框架下自带了丰富的工具类,在我们开发时可以简化很多工作: 1.Resource访问文件资源: 具体有: ResourceUtils.getFile(url); FileSystemReso ...

  5. Spring boot 工具类静态属性注入及多环境配置

    由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此 ...

  6. spring注解工具类AnnotatedElementUtils和AnnotationUtils

    一.前言 spring为开发人员提供了两个搜索注解的工具类,分别是AnnotatedElementUtils和AnnotationUtils.在使用的时候,总是傻傻分不清,什么情况下使用哪一个.于是我 ...

  7. Spring web 工具类 WebApplicationContextUtils

    概述 Spring web 的工具类 WebApplicationContextUtils 位于包 org.springframework.web.context.support 是访问一个Servl ...

  8. Spring的工具类StringUtils使用

    我们经常会对字符串进行操作,spring已经实现了常用的处理功能.我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串. 工具类整理如下:   ...

  9. Spring 常用工具类

    1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...

随机推荐

  1. 笔记-python-centos环境下安装配置

    笔记-python-centos环境下安装配置 1.      准备工作 环境准备 centos6.5 mini,已有python 2.6.6 下载源码包 Python官网下载Gzipped sour ...

  2. selenium2中TestNG相关解释

    testNg官网:http://testng.org/doc/documentation-main.html 新建testNG class的时候,同时也新建了一个TestNG.xml的文件. 此xml ...

  3. Hadoop4.2HDFS测试报告之四

    第二组:文件存储读过程记录 测试系统组成 存储类型 测试程序或命令 测试文件大小(Mb) 文件个数(个) 客户端并发数(个) 读速率 (M/s) NameNode:1 DataNode:1 本地存储 ...

  4. 如何用好 Google 等搜索引擎?

    看见知乎上如何用好Google搜索的问题(http://www.zhihu.com/question/20161362),整理一下.感谢知乎大神 1.Choose Which Google? HTTP ...

  5. Diango 一——URL

    内容概要 1.web框架初识 2.MTV模式 3.Django创建流程和命令行工具 4.配置文件  settings 5.视图函数  views 6.路由系统  URL 7.模板系统  templat ...

  6. day01_12.字符串

    1.字符转义 在字符串中,出现一些比较特殊的字符,容易引起歧义 我们需要转义这些引起歧义的字符串 <?php $a = 'ab\\c'; //转义字符2个 \' \\ $b = 'ab\'c'; ...

  7. poj1236 Tarjan算法模板 详解

    思想: 做一遍DFS,用dfn[i]表示编号为i的节点在DFS过程中的访问序号(也可以叫做开始时间)用low[i]表示i节点DFS过程中i的下方节点所能到达的开始时间最早的节点的开始时间.初始时dfn ...

  8. iOS学习笔记45-Swift(五)协议

    一.Swift协议 协议是为方法.属性等定义一套规范,没有具体的实现,类似于Java中的抽象接口,它只是描述了方法或属性的骨架,而不是实现.方法和属性实现还需要通过定义类,函数和枚举完成. 1. 协议 ...

  9. 许式伟看 Facebook 发币(上): 区块链, 比特币与 Libra 币

    你好,我是七牛云许式伟. Facebook(脸书)于6月18日发布了其加密数字货币项目白皮书.该数字货币被命名为 Libra(天秤座),象征着平衡与公正.此前,BBC 报道说这个数字货币叫 Globa ...

  10. maven项目中配置mvn源

    在pom.xml中配置 <repositories> <!--for others--> <repository> <id>alimaven</i ...