I want to to use boost auto_cpu_timer to display the time my complete code needs to run. Additionally i want to see the progress of my loops using progress_display.
It seems there is a namespace confilict as Boost has two timer classes, where progress_display is from the old, now deprecated library.
http://www.boost.org/doc/libs/1_51_0/libs/timer/doc/index.html
Still, is there a way to achieve this? The following example shows, what i’m trying to do. Using either AUTO or PROG works fine, but both together result in error messages.
Main: compiled with 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;
}
Error log:
/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’
When you include
boost/progress.hpp, the C++ compiler sees a definition forboost::timerasclass timerdefined inboost/timer.hppthat is included inboost/progress.hpp.When you include
boost/time/timer.hpp, the C++ compiler sees another definition forboost::timeras a namespace and this is the cause of the error.If you really want to use it, the solution is renaming one of those
boost::timerthrough macro. But becausenamespace boost::timercontains functions that are implemented outside of the header (likestd::string format(const cpu_times& times, short places, const std::string& format)) you must renameclass boost::timer. So your code will be something like this: