我试图计算循环中函数执行时间的总和,但出现了问题:
#include <boost/chrono.hpp>
#include <iostream>
boost::chrono::milliseconds sumGlobal;
int main() {
boost::chrono::high_resolution_clock::time_point t1 ;
boost::chrono::high_resolution_clock::time_point t2 ;
for (i=0;i<10;i++)
{
t1 = boost::chrono::high_resolution_clock::now();
f(); //to waste time
t2 = boost::chrono::high_resolution_clock::now();
sumGlobal += (boost::chrono::duration_cast<boost::chrono::milliseconds>(t2-t1)).sumGlobal;
}
std::cout << sumGlobal << "\n";
}
错误是:
error: 'struct boost::chrono::duration<long int, boost::ratio<1l, 1000l> >' has no member named 'sumGlobal'
任何解决方案?我认为分配给sumGlobal
的类型应该更改。
答案 0 :(得分:3)
boost::chrono::milliseconds
不会返回带有sumGlobal
数据成员的内容。只是摆脱它。
sumGlobal += (boost::chrono::duration_cast<boost::chrono::milliseconds>(t2-t1));