#include <cstdio>
#include "boost/date_time/posix_time/posix_time.hpp"
int main(int argc, char** argv) {
boost::posix_time::ptime start =
boost::posix_time::microsec_clock::local_time();
uint32_t iterations = 0;
// Do a bunch of work. `iterations` becomes > 0
boost::posix_time::ptime now =
boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration diff = now - start;
printf("Milliseconds per iteration: %f\n",
static_cast<float>(diff.total_milliseconds()) / iterations);
return 0;
}
这打印出负值。怎么会这样?
答案 0 :(得分:4)
使用boost::posix_time::microsec_clock::local_time()
来测量时间差,而夏令时无效,可能会导致所描述的行为。
如果start
设置为01:59:59.345并且程序需要1秒才能执行此操作(在此期间,夏令时无效),now
将设置为01: 00:00.345
您应该使用boost::posix_time::microsec_clock::universal_time()
来避免此问题。