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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM