统计方法运行时间【Java实现】


使用命令模式和适配器模式实现方法运行时间的统计:

 

 

 

 

 

代码实现如下:

 

接口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;
	}
	
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值