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中任意一个方法的执行时间的工具类相关推荐

  1. JAVA有percentile函数吗_计算DAX中的PERCENTILE

    我找到了一种方法,可以通过一系列步骤打破计算,从而获得了一个非常快速的解决方案 . 为了计算表 Data 中 Amount Paid 的第10个百分位数,我遵循了下面的书外公式: Calculate ...

  2. java 字符串转utc时间_在Java中将UTC时间转换为本地时区

    我知道这个主题已被殴打致死,但在搜寻了这个问题几个小时之后,我不得不问. 我的问题:根据客户端应用程序(iphone)的当前时区对服务器上的日期进行计算.客户端应用程序以秒为单位告诉服务器,其时区距G ...

  3. java 取栈顶元素_《Java实战之内存模型》详解篇

    内存是非常重要的系统资源,是硬盘和CPU的中间仓库及桥梁,承载着操作系统和应用程序的实时运行 JVM内存布局规定了Java在运行过程中内存申请.分配.管理的策略,保证了JVM的高效稳定运行 不同的JV ...

  4. java 多态判断非空_收藏Java 面试题全梳理

    脚本之家 你与百万开发者在一起 来源 | Java建设者(ID:javajianshe) 作者 |cxuan 如若转载请联系原公众号 Java 基础篇 Java 有哪些特点 并发性的:你可以在其中执行 ...

  5. java jsp取静态常量_获取java静态

    Android NDK开发系列教程4:对类变量进行操作 终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~ 通常我们 ...

  6. java class文件常量池_《Java虚拟机原理图解》 1.2.3、Class文件中的常量池详解(下)...

    Java内存区域         1.程序计数器(Program Counter Register)(线程私有的)         2.Java虚拟机栈 (Java Virtual Machine S ...

  7. java面试题:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?

    java面试题:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递? 答:是值传递.Java编程语言只有值传递参数. 当一个对象实 ...

  8. 【源码+图片素材】Java王者荣耀游戏开发_开发Java游戏项目【王者荣耀】1天搞定!!!腾讯游戏_Java课程设计_Java实战项目_Java初级项目

    王者荣耀是当下热门手游之一,小伙伴们是否想过如何制作一款属于自己的王者荣耀游戏呢? 本课程讲解了一个王者荣耀游戏的详细编写流程,即使你是刚入门Java的新手,只要你简单掌握了该游戏所需要的JavaSE ...

  9. linux c计算时间差值,获取时间和计算时间差的几种方法总结,时间差几种方法...

    获取时间和计算时间差的几种方法总结,时间差几种方法 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( ...

最新文章

  1. robotium常用方法
  2. 用python查询数据库_用python 做数据库查询
  3. php性能优化二(PHP配置php.ini)
  4. 腾讯陈妍:万物互联时代,保险业应更注重服务创新来挖掘用户需求
  5. 爆牙齿的世界杯日记(小组末轮AB组)
  6. 本文中的问题直接重装mysql即可,但是注意数据也会一并消失
  7. elasticsearch的分布式架构原理
  8. 电脑c盘怎么清理_电脑C盘内存不足?三分钟教你彻底清理C盘空间,瞬间多出10个G...
  9. 编程基本功:正常运行的代码,你看明白能做什么?不如解决几个简单BUG
  10. javascript高级程序设计pdf_Java、C语言、Python、PHP、JavaScript五大编程语言,要学哪个?...
  11. python3迭代器是什么_Python3.7之迭代器
  12. hadoop原理及安装启动
  13. 【记录】在新电脑安装Ubuntu16.04系统并移植虚拟机中catkin_ws过程
  14. 关于当前安全设置不允许下载文件问题的解决
  15. 防火墙开放21端口linux,linux防火墙开放80,3306,21,443端口
  16. 英语介绍计算机,计算机英语自我介绍
  17. blood pressure android app,Blood Pressure Monitor via Bluetooth/Internet in Android
  18. Vue调用后台接口渲染列表
  19. RTU-518G 研究
  20. 陈晨博士|可见光通信LiFi技术简介

热门文章

  1. 关于猿如何找对象,心里没点那啥数吗?
  2. mysql 唯一记录_mysql选择唯一记录
  3. oracle中or会使索引,为何查询条件中多了 OR 选项,索引就走全扫描?
  4. 7个顶级资源网站,不知道就很可惜
  5. PHP中怎样实现正负数的相加,PHP 求任意n个正负整数里面最大的连续和
  6. 降雨插值_ArcGIS计算土壤侵蚀模数(二)降雨侵蚀力因子R计算
  7. java rmi漏洞工具_学生会私房菜【20200924】Weblogic WLS核心组件反序列化命令执行突破(CVE20182628)漏洞复现...
  8. 被嫌弃的eval和with
  9. 如何用纯 CSS 创作一盘传统蚊香
  10. 面向对象程序设计-C++ Default constructor Copy constructor Destructor Operator Overloading【第九次上课笔记】...