更新:代码现在正确编译
我想计算两个时间戳之间的时差.分辨率很重要,因此必须以微秒/毫秒为单位.
我尝试了以下但结果没有意义:
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;
Run Code Online (Sandbox Code Playgroud)
我得到的结果如下:
[2014-Jun-20 12:26:07.711182/2014-Jun-20 12:26:07.711596]
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得以下内容?
76 ?s
Run Code Online (Sandbox Code Playgroud)
你可以使用
std::cout << (now - before).total_microseconds() << " µs\n";
std::cout << (now - before).total_milliseconds() << " ms\n";
Run Code Online (Sandbox Code Playgroud)
这完全符合您的要求(打印例如76 µs
或314 ms
)