我正在尝试使用 clock()
确定 C 函数的运行时间。
这是到目前为止的代码:
time_t start, end;
start = clock();
// Function here
end = clock();
printf("Time was: %lf\n", ((double)(end-start)/CLOCKS_PER_SEC));
它返回 Time was: 0.030000
。如果我添加几秒钟的延迟,它会显示 Time was: 0.500000
。如何让它在毫秒内正确显示?例如。 时间为:500 毫秒
或 时间为:30 毫秒
?
谢谢!
最佳答案
要以毫秒为单位显示时间,请将以秒为单位的时间乘以1000
:
printf("Time was: %d\n", (1000*(end-start)/CLOCKS_PER_SEC));
上面的代码将时间截断到最小的毫秒。
关于c - 使用 clock() 确定函数的运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16095991/