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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值