StopWatch计时器(java小工具)

来源:互联网 发布:单片机系统可靠性 编辑:程序博客网 时间:2024/05/29 18:36
打印每个任务执行时间,以及占总时间百分比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

0 0
原创粉丝点击