posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2008年4月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 865044
          • 排名 - 44

          最新評論

          閱讀排行榜

          如果我們要測試一個類的方法的執行時間,通常我們會這樣做:
          1. public class TestObject {  
          2.     /** 
          3.      * 一個用來被測試的方法,進行了一個比較耗時的循環 
          4.      */  
          5.     public static void testMethod(){  
          6.         for(int i=0; i<100000000; i++){  
          7.               
          8.         }  
          9.     }  
          10.     /** 
          11.      * 一個簡單的測試方法執行時間的方法 
          12.      */  
          13.     public void testTime(){  
          14.         long begin = System.currentTimeMillis();//測試起始時間  
          15.         testMethod();//測試方法  
          16.         long end = System.currentTimeMillis();//測試結束時間  
          17.         System.out.println("[use time]:" + (end - begin));//打印使用時間  
          18.     }  
          19.       
          20.     public static void main(String[] args) {  
          21.         TestObject test=new TestObject();  
          22.         test.testTime();  
          23.     }  
          24. }  
          大家看到了testTime()方法,就只有"http://測試方法"是需要改變的,下面我們來做一個函數實現相同功能但更靈活:
          首先定一個回調接口:
          1. public interface CallBack {  
          2.     //執行回調操作的方法  
          3.     void execute();  
          4. }  
          然后再寫一個工具類:
          1. public class Tools {  
          2.       
          3.     /** 
          4.      * 測試函數使用時間,通過定義CallBack接口的execute方法 
          5.      * @param callBack 
          6.      */  
          7.     public void testTime(CallBack callBack) {  
          8.         long begin = System.currentTimeMillis();//測試起始時間  
          9.         callBack.execute();///進行回調操作  
          10.         long end = System.currentTimeMillis();//測試結束時間  
          11.         System.out.println("[use time]:" + (end - begin));//打印使用時間  
          12.     }  
          13.       
          14.     public static void main(String[] args) {  
          15.         Tools tool = new Tools();  
          16.         tool.testTime(new CallBack(){  
          17.             //定義execute方法  
          18.             public void execute(){  
          19.                 //這里可以加放一個或多個要測試運行時間的方法  
          20.                 TestObject.testMethod();  
          21.             }  
          22.         });  
          23.     }  
          24.       
          25. }  

          posted on 2008-04-01 12:40 周銳 閱讀(966) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 左云县| 东兰县| 乐清市| 砚山县| 北宁市| 抚州市| 德清县| 准格尔旗| 沂源县| 特克斯县| 海伦市| 井冈山市| 沙洋县| 南阳市| 洪泽县| 绍兴县| 玉田县| 青海省| 高州市| 大新县| 金湖县| 囊谦县| 南乐县| 鄂尔多斯市| 乳源| 唐山市| 青铜峡市| 潢川县| 沧州市| 濮阳市| 会宁县| 崇阳县| 元阳县| 屏边| 莆田市| 高陵县| 宜兰市| 甘孜| 航空| 台南市| 海原县|