#include#include clock_t start, stop;//clock_t为clock()函数返回的变量类型 double duration;//记录被测函数运行时间,以秒为单位 int main() { start = clock();//开始计时 //运行代码段 // stop = clock();//停止计时 duration = (double)(stop - start) / CLOCKS_PER_SEC;//计算运行时间 printf("%fn", duration); return 0; }
clock()函数,头文件为
CLOCKS_PER_SEC。表示一秒钟内CPU运行的始终周期数。
其中CLOCKS_PER_SEC 和 CLK_TCK 是等效的,不过后者在ubuntu中编译报错,最好用
CLOCKS_PER_SEC。



