加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

c – 为什么gettimeofday()间隔偶尔会消极?

发布时间:2020-05-23 11:58:11 所属栏目:Linux 来源:互联网
导读:我有一个实验性的库,我试图测量它的性能.为此,我写了以下内容:struct timeval begin; gettimeofday(begin, NULL); { // Experiment! } struct timeval end; gettimeofday(end, NULL); // Print the time it

我有一个实验性的库,我试图测量它的性能.为此,我写了以下内容:

struct timeval begin;
gettimeofday(&begin,NULL);
{
    // Experiment!
}
struct timeval end;
gettimeofday(&end,NULL);

// Print the time it took!
std::cout << "Time: " << 100000 * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) << std::endl;

偶尔,我的结果包括负面时间,其中一些是荒谬的.例如:

Time: 226762
Time: 220222
Time: 210883
Time: -688976

这是怎么回事? 最佳答案 你有一个错字.更正了最后一行(注意0的数量):

std::cout << "Time: " << 1000000 * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) << std::endl;

顺便说一下,timersub是一种内置方法,用于获取两个时间间隔之间的差异.

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读