统计方法运行时间【Java实现】
来源:互联网 发布:stc下载软件 编辑:程序博客网 时间:2024/06/11 20:21
使用命令模式和适配器模式实现方法运行时间的统计:
代码实现如下:
接口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实现】
- java统计运行时间
- java 统计程序运行时间
- c++统计程序运行时间的方法
- js实现网站运行时间统计
- Java统计程序运行时间(转)
- 统计代码运行时间
- 统计程序运行时间
- CUDA统计运行时间
- CCS上程序运行时间统计方法最终报告
- CCS上程序运行时间统计方法最终报告
- CCS上程序运行时间统计方法最终报告
- C/C++/Qt软件系统统计运行时间方法详解
- Java多线程下统计多个线程的运行时间
- java测试方法运行时间 System.currentTimeMillis();
- java如何记录方法运行时间?
- 检测JAVA程序的运行时间方法
- java代码运行时间超时exception方法
- 回家
- 我已经进入东南融通公司工作。
- 如何判断地址栏?传值是否存在
- DLLMAIN() 详解
- 私の趣味
- 统计方法运行时间【Java实现】
- 面试和笔试
- 如何隐藏nginx版本号来提高安全性
- 变态hello word(带注释)
- 自动调用webservice_axis
- 传值还是传地址
- WPF中LISTITEM的拖拽
- USACO算法系列十九——fence4
- C/C++面试题(一)