日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java方法执行的时间_计算Java中任意一个方法的执行时间的工具类

發布時間:2023/12/1 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 }

總結

以上是生活随笔為你收集整理的java方法执行的时间_计算Java中任意一个方法的执行时间的工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。