language c / c ++ runtime calculation program

Accurate to milliseconds (ms)

#include<stdio.h>
#include<time.h>   //用到clock()函数
int main() {
    int begintime,endtime;
    int i = 0;
    int a[1002];
    begintime=clock();  //计时开始
    

        /*
        代码块
        */

    endtime = clock();  //计时结束
    printf("\n\nRunning Time:%dms\n", endtime-begintime);
    return 0;
}

Accurate to the microsecond (us)

#include<stdio.h>
#include <windows.h>
int main() {
    int a[10002];
    int i = 0;
    double run_time;
    _LARGE_INTEGER time_start;  //开始时间
    _LARGE_INTEGER time_over;   //结束时间
    double dqFreq;      //计时器频率
    LARGE_INTEGER f;    //计时器频率
    QueryPerformanceFrequency(&f);
    dqFreq=(double)f.QuadPart;
    QueryPerformanceCounter(&time_start);   //计时开始
    

        /*
        代码句
        */


    QueryPerformanceCounter(&time_over);    //计时结束
    run_time=1000000*(time_over.QuadPart-time_start.QuadPart)/dqFreq;
    //乘以1000000把单位由秒化为微秒,精度为1000 000/(cpu主频)微秒
    printf("\nrun_time:%fus\n",run_time);
    return 0;
}

Testing time relationship with the n

Mode uses a simple y ++

The time complexity is O (n)
perform operations 9

#include<stdio.h>
#include <iostream>
#include <windows.h>
#define ll long long
using namespace std;
int main() {


    ll x=1000;
    for(int i=1;i<10;i++){
        double run_time;
        LARGE_INTEGER time_start;  //开始时间
        LARGE_INTEGER time_over;   //结束时间
        double dqFreq;      //计时器频率
        LARGE_INTEGER f;    //计时器频率
        QueryPerformanceFrequency(&f);
        dqFreq=(double)f.QuadPart;
        QueryPerformanceCounter(&time_start);   //计时开始

        //开始操作
        ll y=0;
        for(ll j=1;j<=x;j++){
            y++;
        }
        x*=10;
        //结束操作

        QueryPerformanceCounter(&time_over);    //计时结束
        run_time=1000000*(time_over.QuadPart-time_start.QuadPart)/dqFreq;
        //乘以1000000把单位由秒化为微秒,精度为1000 000/(cpu主频)微秒
        printf("\nrun_time:%fus\n",run_time);
    }
    return 0;
}

The final output is

run_time:1.924813us

run_time:19.248130us

run_time:192.802098us

run_time:1950.156327us

run_time:18903.267242us

run_time:179787.474985us

run_time:1795408.102051us

run_time:18517596.319244us

run_time:186273317.344554us

Guess you like

Origin www.cnblogs.com/Emcikem/p/11518388.html