使用boost :: chrono计算执行时间

时间:2015-06-18 08:09:22

标签: c++ boost type-conversion global-variables

我试图计算循环中函数执行时间的总和,但出现了问题:

#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的类型应该更改。

1 个答案:

答案 0 :(得分:3)

boost::chrono::milliseconds不会返回带有sumGlobal数据成员的内容。只是摆脱它。

sumGlobal += (boost::chrono::duration_cast<boost::chrono::milliseconds>(t2-t1));