統計方法運行時間【Java實現】

   接口Command:定義命令的執行操做java

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: 測量組合問題的運行時間   this

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();
        }
        
    }

}

 

  另一種使用反射機制實現的運行時間測量框架:spa

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;
    }
    
}
相關文章
相關標籤/搜索