我想使用 boostauto_cpu_timer
来显示我的完整代码需要运行的时间。此外,我想查看我的循环使用的进度progress_display
。
似乎存在命名空间冲突,因为 Boost 有两个计时器类,其中progress_display
来自旧的、现已弃用的库。
http://www.boost.org/doc/libs/1_51_0/libs/timer/doc/index.html
不过,有没有办法实现这一目标?以下示例显示了我正在尝试做的事情。使用其中一个AUTO
或PROG
工作正常,但两者一起导致错误消息。
主要:编译与g++ -lboost_timer main.cc -o time
#define AUTO
#define PROG
#ifdef PROG
#include <boost/progress.hpp>
#endif //---- PROG -----
#ifdef AUTO
#include <boost/timer/timer.hpp>
#endif //---- AUTO -----
#include <cmath>
int main()
{
#ifdef AUTO
boost::timer::auto_cpu_timer t;
#endif //---- AUTO -----
long loops = 100000000;
#ifdef PROG
boost::progress_display pd( loops );
#endif //---- PROG -----
//long loop to burn some time
for (long i = 0; i < loops; ++i)
{
std::sqrt(123.456L);
#ifdef PROG
++pd;
#endif //---- PROG -----
}
return 0;
}
错误日志:
/usr/include/boost/timer/timer.hpp:38:1: error: ‘namespace boost::timer { }’ redeclared as different kind of symbol
/usr/include/boost/timer.hpp:45:1: error: previous declaration of ‘class boost::timer’
/usr/include/boost/timer/timer.hpp: In member function ‘std::string boost::timer::cpu_timer::format(short int, const std::string&) const’:
/usr/include/boost/timer/timer.hpp:74:34: error: ‘format’ is not a member of ‘boost::timer’
/usr/include/boost/timer/timer.hpp: In member function ‘std::string boost::timer::cpu_timer::format(short int) const’:
/usr/include/boost/timer/timer.hpp:76:34: error: ‘format’ is not a member of ‘boost::timer’
main.cc: In function ‘int main()’:
main.cc:17:2: error: ‘auto_cpu_timer’ is not a member of ‘boost::timer’
main.cc:17:31: error: expected ‘;’ before ‘t’