统计方法运行时间【Java实现】
2010-12-13 21:35
691 查看
使用命令模式和适配器模式实现方法运行时间的统计:
代码实现如下:
接口Command:定义命令的执行操作
CommandRuntime 类:统计命令运行时间,使用命令模式
CombinationCommand: 解决组合问题的命令, 采用类适配器模式:
CombinationRuntime: 测量组合问题的运行时间
另外一种使用反射机制实现的运行时间测量框架:
代码实现如下:
接口Command:定义命令的执行操作
package common; public interface Command { // 运行方法 void run(); }
CommandRuntime 类:统计命令运行时间,使用命令模式
package common; public class CommandRuntime { private Command command; public CommandRuntime(Command command) { this.command = command; } public long runtime() { long start = System.currentTimeMillis(); command.run(); long end = System.currentTimeMillis(); return end-start; } }
CombinationCommand: 解决组合问题的命令, 采用类适配器模式:
package algorithm.problems; import algorithm.permutation.Combination; import common.Command; public class CombinationCommand extends Combination implements Command { public CombinationCommand(int n) { super(n); } public void run() { solution(); } }
CombinationRuntime: 测量组合问题的运行时间
package algorithm.problems; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import common.CommandRuntime; public class CombinationRuntime { public static void main(String[] args) { try { BufferedWriter fileWriter = new BufferedWriter(new FileWriter("runtime.txt")); fileWriter.write("runtime: "); fileWriter.newLine(); for (int i=1; i < 30; i++) { CommandRuntime comRuntime = new CommandRuntime(new CombinationCommand(i)); long runtime = comRuntime.runtime(); fileWriter.write( "n = " + i + " : " + runtime + " ms"); fileWriter.newLine(); } System.out.println("over."); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
另外一种使用反射机制实现的运行时间测量框架:
package common; import java.lang.reflect.Constructor; import java.lang.reflect.Method; public class RuntimeMeasurement { public RuntimeMeasurement(int maxsize) { this.maxsize = maxsize; time = new double[maxsize]; } // 问题最大规模: 以 10 的 size 次幂计 private int maxsize ; // 运行时间以 ms 计 private double[] time ; /** * measureTime : 对指定类型的对象调用指定参数列表的指定方法,并测量其运行时间 * @param type 指定对象类型,必须有一个 参数类型为 int 的公共构造器方法 * @param methodName 指定测试方法名称,要求是空参数列表 */ public void measureTime(Class<?> type, String methodName) { try { Constructor<?> con = type.getConstructor(int.class); Method testMethod = null; for (int i = 0; i < time.length; i++) { Object obj = con.newInstance(power10(i+1)); testMethod = type.getMethod(methodName, new Class<?>[]{}); long start = System.nanoTime(); testMethod.invoke(obj, new Object[] {}); long end = System.nanoTime(); time[i] = ((end - start) / (double)1000000) ; } } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage()); } } /** * showTime : 显示已经测量获得的运行时间,在 measureTime 方法调用后调用该方法。 */ public void showTime() { for (int i=0; i < time.length; i++) { System.out.printf("n = %12d : " , power10(i+1)); System.out.printf("%12.3f\n", time[i]); } } private int power10(int n) { int result = 1; while (n > 0) { result *= 10; n--; } return result; } }
相关文章推荐
- 统计方法运行时间【Java实现】
- springAOP实现方法运行时间统计
- Python实现__metaclass__实现方法运行时间统计
- java线程控制,当stop按钮就停止,当start按钮就开始运行,当按stop时程序又停止的实现方法
- Java实现字符串解析为日期时间的方法示例
- Java统计程序运行时间
- 刘一天@认真是一种态度 使劲的浪费,用力的后悔。 java监测方法运行时间/效率方法
- 【JAVA开发技术进阶】 Java实现运行任意目录下class中加了@MyTest的空参方法(实现图形界面)
- java 外部调用c程序(后缀为.exe--是可以运行的程序--不是本地方法的实现)
- java 定时器,固定某个时间执行(Spring+Quartz实现定时任务的配置方法)
- 使用javassist 无侵入实现方法时间统计
- Java统计程序运行时间(转)
- Java统计程序运行时间(转)
- Java实现时间动态显示方法汇总
- java测试方法和代码的运行时间
- java如何监控一个方法的运行时间 [问题点数
- Java---注解、类加载器-加强-实现运行任意目录下class中加了@MyTest的空参方法
- 利用AOP实现对方法执行时间的统计
- 实现Java程序间隔时间运行
- Java实现纪元秒和本地日期时间互换的方法【经典实例】