在C中测试函数运行时间

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

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

int main(int argc, char **argv)
{
    /* 不再测试范围内的准备工作写在clock()调用之前 */

    //开始计时
    start = clock();
    //被测量的函数

    //停止计时
    stop = clock();
    //计算运行时间
    duration = ((double)(stop - start)) / CLOCKS_PER_SEC;

    /* 其他不在测试范围的处理写在后面, 例如输出duration的值 */
    printf("Running time: %6.2es.\n", duration);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Will-zyq/p/11329307.html