Use command mode and adapter mode to calculate the running time of the method:
The code is implemented as follows:
Interface command: defines the execution of commands
Package common; public interface command {// running method void run ();}
Commandruntime: used to count the running time of a command.
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: the command to solve the combination problem. The class adapter mode is used:
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: measure the running time of a combination problem.
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();}}}
Another runtime measurement framework using the reflection mechanism:
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];} // maximum problem scale: Calculate private int maxsize with 10 power; // run time in MS private double [] Time;/*** measuretime: calls the specified method of the specified parameter list for an object of the specified type, and measure the running time * @ Param type specifies the object type, there must be a public constructor method with the parameter type of int * @ Param meth Odname specifies the name of the test method, which must be an empty parameter list */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: displays the measured running time. This method is called after the measuretime method is called. */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 ;}}