java方法執行的時間_計算Java中任意一個方法的執行時間的工具類

1 packagealgorithm.study.utils;2

3 importjava.lang.reflect.Method;4

5 /**

6 * This class is getting a method execute time and provide some other functions.7 *8 *@authorygh 2017年2月24日9 */

10 public classMethodExecuteTimeUtils {11

12 /**

13 * Get a method execute time using millisecond and cancel method's print return result and14 * execute time.15 *16 *@parambean The method is in this bean17 *@paramparams The parameter the method execute need18 *@parammethodName The name of the method19 *@paramtypes The parameter type of the method20 *@returnThe execute time of this method21 *@throwsException If getMethod or invoke fail.22 */

23 public static long getMethodExecuteTime(Object bean, Object[] params, String methodName, Class>[] types)24 throwsException {25 return getMethodExecuteTime(bean, params, methodName, types, false, false);26 }27

28 /**

29 * Get a method execute time using millisecond and cancel print method's return result.30 *31 *@parambean The method is in this bean32 *@paramparams The parameter the method execute need33 *@parammethodName The name of the method34 *@paramtypes The parameter type of the method35 *@paramisPrintExecutetime Whether print the execute time in console, true is print false not36 *@paramisViewMehtodResult Whether print the return result in console, true is print false not37 *@returnThe execute time of this method38 *@throwsException If getMethod or invoke fail.39 */

40 public static longgetMethodExecuteTime(Object bean, Object[] params, String methodName,41 Class>[] types, boolean isPrintExecutetime) throwsException {42 return getMethodExecuteTime(bean, params, methodName, types, isPrintExecutetime, false);43 }44

45 /**

46 * Get a method execute time using millisecond and add some other service.47 *48 *@parambean The method is in this bean49 *@paramparams The parameter the method execute need50 *@parammethodName The name of the method51 *@paramtypes The parameter type of the method52 *@paramisPrintExecutetime Whether print the execute time in console, true is print false not53 *@paramisViewMehtodResult Whether print the return result in console, true is print false not54 *@returnThe execute time of this method55 *@throwsException If getMethod or invoke fail.56 */

57 public static longgetMethodExecuteTime(Object bean, Object[] params, String methodName,58 Class>[] types, boolean isPrintExecutetime, boolean isViewMehtodResult) throwsException {59 Class>clazz;60 long executeTime = -1L;61 boolean isAccessiable = false;62 Method method = null;63 if (bean instanceof Class>) {64 clazz = (Class>) bean;65 } else{66 clazz =bean.getClass();67 }68 try{69 if (types == null) {70 method =clazz.getDeclaredMethod(methodName);71 } else{72 method =clazz.getDeclaredMethod(methodName, types);73 }74 isAccessiable =method.isAccessible();75 if (!isAccessiable) {76 method.setAccessible(true);77 }78

79 if(isViewMehtodResult) {80 executeTime =getReturnMethodExecuteTime(bean, params, method);81 } else{82 executeTime =getMethodExecuteTime(bean, params, method);83 }84 method.setAccessible(isAccessiable);85 if(isPrintExecutetime) {86 printExecute(clazz, methodName, executeTime);87 }88 } catch(Exception e) {89 throw new Exception("excute method fail");90 }91 returnexecuteTime;92 }93

94 /**

95 * Get a method execute time, use millisecond. We don't think the method whether has a return96 * result97 *98 *@parambean The method is in this bean99 *@paramparams The parameters the method execute need100 *@parammethod The Method entity101 *@returnThe millisecond the method execute spend102 *@throwsException If the method invoke fail103 */

104 private static long getMethodExecuteTime(Object bean, Object[] params, Method method) throwsException {105 long startTime =System.currentTimeMillis();106 method.invoke(bean, params);107 long endTime =System.currentTimeMillis();108 return endTime -startTime;109 }110

111 /**

112 * Get a method execute time, use millisecond. The method must has a return result will input113 * the return result in console ,If the method has not return result, please call114 * getMethodExecuteTime method.115 *116 *@parambean The method is in this bean117 *@paramparams The parameters the method execute need118 *@parammethod The Method entity119 *@returnThe millisecond the method execute spend120 *@throwsException If the method invoke fail121 */

122 private static longgetReturnMethodExecuteTime(Object bean, Object[] params, Method method)123 throwsException {124 long startTime =System.currentTimeMillis();125 Object result =(Object) method.invoke(bean, params);126 long endTime =System.currentTimeMillis();127 if (result != null) {128 System.out.println("result input:" +result.toString());129 } else{130 System.out.println("Warning:" + bean.getClass().getName() + "." +method.getName()131 + "has not return " + "result,please setting the isViewMehtodResult as false");132 }133 return endTime -startTime;134 }135

136 /**

137 * Print the execute time of method138 *139 *@parammethodName The name of the method140 *@paramtime The execute of the method141 */

142 public static void printExecute(Class> clazz, String methodName, longtime) {143 System.out.println(clazz.getName() + "." + methodName + " execute time: " +time);144 }145 }

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373360.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373360.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373360.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何在 IIS 中設置 HTTPS 服務

Windows Server2008、IIS7啟用CA認證及證書制作完整過程 這篇文章介紹了如何安裝證書申請工具; 如何在iis創建證書申請; 如何使用iis申請證書生成的txt文件,在工具中開始申請證書; 如何導出證書; 以及在網站中開始使用…

Android之衛星菜單的實現

衛星菜單是現在一個非常受歡迎的“控件”,很多Android程序員都趨之若鶩,預覽如下圖。傳統的衛星菜單是用Animation實現的,需要大量的代碼,而且算法極多,一不小心就要通宵Debug。本帖貼出用屬性動畫Animator來實現衛星菜…

Java中的WADL:溫和的介紹

WADL( Web應用程序描述語言 )對REST而言,WSDL對SOAP而言。 這種語言的僅僅存在引起了很多爭議(請參閱: 我們需要WADL嗎? 或者 需要 WADL還是不需要WADL )。 我可以想到使用WADL的一些合法用例&a…

類成員函數模板特化

//類成員函數模板特化 #include <stdio.h> class A{ public:template <class T>void Print(){printf("A template\n");} };template<> void A::Print<int>(){printf("int\n"); }int main(){A a;a.Print<double>();a.Print&l…

為云量身定制您的服務

相信大家都聽說過Amazon的AWS。作為業內最為成熟的云服務提供商&#xff0c;其運行規模&#xff0c;穩定性&#xff0c;安全性都已經經過了市場的考驗。時至今日&#xff0c;越來越多的應用被部署在了AWS之上。這其中不乏Zynga及Netflix這樣著名的服務。 然而這一切并沒有停滯不…

在Vaadin和JSF之間選擇

隨著最新版本的Primefaces 3.0的發布&#xff0c;JSF終于達到了前所未有的成熟度和實用性&#xff0c;使其與其他流行的Rich Internet Applications&#xff08;RIA&#xff09;選項如Google Web Toolkit&#xff08;GWT&#xff09;&#xff0c;ExtJS&#xff0c;Vaadin&#…

20145202馬超《信息安全系統設計基礎》實驗二總結

[實驗二]&#xff08;http://www.cnblogs.com/nizaikanwoma/p/6131778.html&#xff09; 轉載于:https://www.cnblogs.com/tuolemi/p/6131987.html

java 連接ldap_ldap java 連接demo

public class LDAPHelper {/*** LDAP可以理解為一個多級目錄&#xff0c;這里&#xff0c;表示要連接到那個具體的目錄*/private final String baseDn "ouPeople,dcchangyeyi,dccom";private LdapContext ctx null;private final Control[] connCtls null;private…

flask開發restful api系列(1)

在此之前&#xff0c;向大家說明的是&#xff0c;我們整個框架用的是flask sqlalchemy redis。如果沒有開發過web&#xff0c;還是先去學習一下&#xff0c;這邊只是介紹如果從開發web轉換到開發移動端。如果flask還不是很熟悉&#xff0c;我建議先到這個網站簡單學習一下&am…

Apache Commons Lang StringUtils

因此&#xff0c;認為最好談論我喜歡的另一個Java庫。 它已經存在了一段時間&#xff0c;也許不是最令人興奮的庫&#xff0c;但是它非常有用。 我可能每天都使用它。 org.apache.commons.lang.StringUtils StringUtils是Apache Commons Lang&#xff08; http://commons.apac…

JEE7:展望新時代

計劃于2012年下半年發布的Java EE 7預計的JSR都已啟動并正在運行。 Java EE 7發行版是日期驅動的&#xff0c;它將反映該行業遷移到云中時不斷變化的需求&#xff1a;任何未準備就緒的內容將推遲到Java EE 8中使用 。 這是Java EE 7平臺中不同規范的關鍵功能的更新和摘要。 1。…

Cocos2d-JS項目之UI界面的優化

測試環境&#xff1a; iphone4、iOS6.1.2、chrome 37.2062.60&#xff0c;Cocos2d-js 3.6 之前寫了不少&#xff0c;實際項目也按這個去優化了&#xff0c;也有效果&#xff0c;但到最后才發現&#xff0c;尼瑪&#xff0c;之前都搞錯了&#xff0c;之所以有效果是歪打正著。。…

java數_java大數

java大數還是很好用的&#xff01;基本加入&#xff1a;import java.math.BigInteger;import jave.math.BigDecimal;分別是大數和大浮點數。首先讀入可以用&#xff1a;Scanner input new Scanner(System.in);BigInteger a input.nextBigInteger();這樣讀還是很方便的當然還有…

【Qt之Quick模塊】6. QML語法詳解_2類型系統

描述 在QML文檔中對象層次結構的定義中可能使用的類型可以來自各種來源。它們可能是: 由QML語言原生提供通過QML模塊通過c注冊由QML模塊作為QML文檔提供 此外&#xff0c;應用程序開發人員可以通過直接注冊c類型&#xff0c;或者通過在QML文檔中定義可重用的組件(然后可以導…

JS顯示當前時間(包含農歷時間)

時間格式&#xff1a; JavaScript代碼&#xff1a; var sWeek new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");var dNow new Date();var CalendarData new Arra…

Maven原型創建技巧

我最近需要為姜黃SOA項目創建一些Maven原型。 對于不了解的人來說&#xff0c; Maven原型是一種基于一些預先罐裝的項目模板生成項目的方法。 對于當前的姜黃SOA原型&#xff0c;它將創建一個多模塊Maven項目&#xff0c;該項目包含Interface和Service項目以及基本的WSDL和適當…

MyBatis操作指南-與Spring集成(基于注解)

轉載于:https://www.cnblogs.com/weilu2/p/mybatis_spring_integration_basic_on_annotation.html

Windows mysql boost_Win7下Boost庫的安裝

Boost庫是C領域公認的經過千錘百煉的知名C類庫&#xff0c;涉及編程中的方方面面&#xff0c;簡單記錄一下使用時的安裝過程1.boost庫的下載boost庫官網主頁&#xff1a;www.boost.org2.安裝將下載的壓縮包解壓到指定的目錄3.建立編譯工具bjam.exe在源碼目錄下執行bootstrap.ba…

5.2與終端進行對話

Linux提供了一個特殊的設備 /dev/tty &#xff0c;該設備始終是指向當前終端或者當前的登錄會話。 FILE* output fopen("/dev/tty", "w"); //向終端寫入字符串 fprintf(output, "%s\n", "world"); FILE* input fopen("/dev/tty…

JVM:如何分析線程轉儲

本文將教您如何分析JVM線程轉儲&#xff0c;并查明問題的根本原因。 從我的角度來看&#xff0c;線程轉儲分析是掌握Java EE生產支持的任何個人最重要的技能。 您可以從線程轉儲快照中獲取的信息量通常遠遠超出您的想象。 我的目標是與您分享我在過去10年中積累的有關線程轉儲分…