C ++在Linux上获取毫秒时间 - clock()似乎无法正常工作 [英] C++ obtaining milliseconds time on Linux -- clock() doesn't seem to work properly

查看:521
本文介绍了C ++在Linux上获取毫秒时间 - clock()似乎无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上, clock()返回以毫秒为单位的时间,但在我正在处理的这个Linux机器上,它舍入到最接近的1000,因此精度



我发现一个使用 QTime 的Qt解决方案>类,实例化一个对象并调用 start(),然后调用 elapsed()已过。



我有幸运,因为我与Qt一起工作,但我想要一个不依赖第三方库的解决方案,



是否有标准方法?



UPDATE >

请不要推荐Boost ..



如果Boost和Qt可以做到,肯定不是魔法,

解决方案

您可以在方法的开始和结束处使用gettimeofday,两个返回结构。您将得到如下的结构:

  struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
}


On Windows, clock() returns the time in milliseconds, but on this Linux box I'm working on, it rounds it to the nearest 1000 so the precision is only to the "second" level and not to the milliseconds level.

I found a solution with Qt using the QTime class, instantiating an object and calling start() on it then calling elapsed() to get the number of milliseconds elapsed.

I got kind of lucky because I'm working with Qt to begin with, but I'd like a solution that doesn't rely on third party libraries,

Is there no standard way to do this?

UPDATE

Please don't recommend Boost ..

If Boost and Qt can do it, surely it's not magic, there must be something standard that they're using!

解决方案

You could use gettimeofday at the start and end of your method and then difference the two return structs. You'll get a structure like the following:

struct timeval {
  time_t tv_sec;
  suseconds_t tv_usec;
}

这篇关于C ++在Linux上获取毫秒时间 - clock()似乎无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆