【发布时间】:2017-06-20 20:39:08
【问题描述】:
我调用了“sleep”函数,但似乎并没有真正计算在内,像这样:
#include<iostream>
#include<unistd.h>
#include<time.h>
using namespace std;
int main(){
clock_t t1=clock();
sleep(2);
clock_t t2=clock();
cout<<t2-t1<<endl;
return 0;
}
它会在 2 个“clock()”调用之间打印非常少的毫秒时间间隔。我预计它应该报告 2,000,000。
我的猜测正确吗?手册页没有对我的问题指定任何答案。有没有更好的函数来获取两行代码之间经过的时间?
【问题讨论】:
-
使用 gettimeofday()
标签: linux function time process clock