zoukankan      html  css  js  c++  java
  • spring计时工具类stopwatch用法

    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-- > 0) {  
                sum += i;  
            }  
            System.out.println(sum);  
        }  
      
        private static void method2() {  
            int i = Integer.MAX_VALUE;  
            long sum = 0l;  
            while ((i -= 5) > 0) {  
                sum += i;  
            }  
            System.out.println(sum);  
        }  
    }  
    output:
    2305843005992468481
    461168600339500238
    StopWatch 'test': running time (millis) = 1566
    -----------------------------------------
    ms     %     Task name
    -----------------------------------------
    01428  091%  method1
    00138  009%  method2
  • 相关阅读:
    我要AFO啦好伤感啊
    noip2012~2015刷题小记录
    【20161114模拟赛】
    第5模块闯关CSS练习题
    HTML练习题
    Mysql常用命令行大全
    mysql破解密码安装与基本管理
    python 闯关之路四(下)(并发编程与数据库编程) 并发编程重点
    Python/ selectors模块及队列
    python3 中 Event.wait 多线程等待
  • 原文地址:https://www.cnblogs.com/panxuejun/p/7839112.html
Copyright © 2011-2022 走看看