C程序计算耗费的时间

来源:互联网 发布:一线房价 知乎 编辑:程序博客网 时间:2024/04/27 15:09

使用 clock()函数读取CPU的开始,结束时针, 计算时针差。

然后除以CLOCKS_PER_SEC常量,折算成秒。

 

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

main()
{

    clock_t  start, end;

 

    // waiting time

    double tt = 4.7;

    start = clock();

    // waiting
    while((start + 4.7*CLOCKS_PER_SEC) > clock());

    end = clock();

    printf("/n Resutlt: %.2f  seconds. ", (double)(end-start)/CLOCKS_PER_SEC);

}

原创粉丝点击