加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何使用Boost库计算两个时间戳之间的微秒时差

发布时间:2020-12-16 09:52:48 所属栏目:百科 来源:网络整理
导读:更新:代码现在正确编译 我想计算两个时间戳之间的时差.分辨率很重要,因此必须以微秒/毫秒为单位. 我尝试了以下但结果没有意义: boost::posix_time::ptime before = (input[0])-timestamp;boost::posix_time::ptime now = boost::posix_time::microsec_cloc
更新:代码现在正确编译

我想计算两个时间戳之间的时差.分辨率很重要,因此必须以微秒/毫秒为单位.

我尝试了以下但结果没有意义:

boost::posix_time::ptime before = (&input[0])->timestamp;
boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();

boost::posix_time::time_period tp (before,now);
std::string str (boost::posix_time::to_simple_string (tp));
cout << str.c_str() << endl;

我得到的结果如下:

[2014-Jun-20 12:26:07.711182/2014-Jun-20 12:26:07.711596]

我怎样才能获得以下内容?

76 μs

解决方法

你可以使用

std::cout << (now - before).total_microseconds() << " μsn";
std::cout << (now - before).total_milliseconds() << " msn";

这完全符合您的要求(打印例如76μs或314 ms)

(编辑:李大同)

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

    推荐文章
      热点阅读