C: clock () computing code execution time

clock (): capture from the program starts running to clock () is invoked consuming event.
This time the unit is clock tick, that is, the clock RBI
constant CLK_TCK: machine clock go hit points per second clock

To use this function needs to include the header file time.h

#include <stdio.h>
#include <time.h>

// clock_t 是clock()函数的返回值类型
clock_t start, stop;
// 记录被测代码的运行时间,以秒为单位
double duration;

int main()
{
    // 记录开始时间
    start = clock();
    //......代码
    // 记录结束时间
    stop = clock();
    // 计算代码执行花费的时间
    duration = ((double)(stop-start)) / CLK_TCK;

    return 0;
}

Sometimes the test code executed quickly, no comparison between the speed of execution
we can make the code under test is repeated a plurality of times sufficient to run, so that the total dot clock measured interval is sufficiently long, the final calculation of the average per-function test running time to

Guess you like

Origin www.cnblogs.com/wbyixx/p/12229942.html