#include <iostream>
#include <iomanip>
#include <windows.h>
#include <math.h>
#define num 20
#define cyc 1000000000ul 
using namespace std;
int main(){
    LARGE_INTEGER startTime, endTime, fre;
    double times[num], timeAvg, timeSd;
    unsigned int i;
    int n;
    int p=1234;
    int q;
    QueryPerformanceFrequency(&fre); //取得CPU頻率
    printf("執行%3d輪，每輪執行 %d次\n\n", num, cyc);
    for (n = 0; n < num; n++){
        QueryPerformanceCounter(&startTime); //取得開機到現在經過幾個CPU Cycle
        for(i = 0; i < cyc; i++){
            //↓輸入測試程式↓ 
            q = p*2;
            //↑輸入測試程式↑ 
        }
        QueryPerformanceCounter(&endTime); //取得開機到程式執行完成經過幾個CPU Cycle
        times[n] = ((double)endTime.QuadPart - (double)startTime.QuadPart)/fre.QuadPart;
    }
    timeAvg = 0;
    timeSd = 0;
    for (n = 0; n < num; n++){
        timeAvg += times[n];
        printf("第%3d輪所需時間%15.10f秒\n", n + 1, times[n]);
    }
    timeAvg /= num;
    for (n = 0; n < num; n++){
        timeSd += (times[n] - timeAvg)*(times[n] - timeAvg);
    }
    timeSd = sqrt(timeSd / num);
    printf("\n執行完畢，\n平均執行時間%15.10f秒，\n標準差%15.10f秒\n", timeAvg, timeSd);
    system("pause");
    return 0;
}
