如何在linux中测量时间?

use*_*863 2 c linux timer

我想在给定时间参数时将数据写入文件:例如,我得到x = 7 - >意味着在接下来的7秒内将一些随机数据写入文件我遇到了一些困难,我尝试过使用:clock( )和struct timeval但它不起作用

我试过的事情:

struct timeval start, end;
gettimeofday(&start, 0);
while(  gettimeofday(&end, 0) && end.tv_sec - start.tv_sec < sec )
  {
    write....
  }
Run Code Online (Sandbox Code Playgroud)

但它会停止时钟..

我很乐意得到一些帮助.谢谢

MBy*_*ByD 7

如果getTimeOfDay成功,则返回0,然后您的while条件失败.尝试:

while(  (gettimeofday(&end, 0) == 0) && end.tv_sec - start.tv_sec < sec )
{
    write....
}
Run Code Online (Sandbox Code Playgroud)