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. Linux学习-编译前的任务:认识核心与取得核心原始码

    什么是核心 (Kernel) Kernel 其实核心就是系统上面的一个文件而已, 这个文件包含了驱动主机各项硬 件的侦测程序与驱动模块. 核心文件通常被放置成 /boot/vmlinuz-xxx ,不 ...

  2. winServer08上安装SQL时提示“必须使用管理角色安装”或配置microsoft.net framework 3.5

    server 2008安装vs2008后报错,如图: 解决方法: 控制面板—>程序—>打开或关闭Windows功能—>进入服务器管理器选择功能—>添加功能 然后勾选.NET F ...

  3. SMP IRQ Affinity

    转:非常有用的方法,调式神器 SMP IRQ Affinity Background: Whenever a piece of hardware, such as disk controller or ...

  4. 可持久化treap(FHQ treap)

    FHQ treap 的整理 treap = tree + heap,即同时满足二叉搜索树和堆的性质. 为了使树尽可能的保证两边的大小平衡,所以有一个key值,使他满足堆得性质,来维护树的平衡,key值 ...

  5. js---JSONP原理及使用

    极简解释: 利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的.当需要通讯时,本站脚本创建一个<script>元素,地址指向第三方的API网址 ...

  6. IE6 下绝对定位position:absolute 与浮动不显示 (IE6 下拉菜单显示)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD& ...

  7. koa2源码解读

    最近在复习node的基础知识,于是看了看koa2的源码,写此文分享一下包括了Koa2的使用.中间件及上下文对象的大致实现原理. koa的github地址:https://github.com/koaj ...

  8. 【CCF】网络延时 树搜索

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  9. linux centos7无法连接ssh

    在centos7连接ssh时,参考了以下博文,终于完美解决 https://blog.csdn.net/trackle400/article/details/52755571 1.  首先,要确保Ce ...

  10. hdu 3501 容斥原理或欧拉函数

    Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...