// Extend the timing accuracy through progress_timer // in principle Program Library Code Cannot be modified by users, but we can use the template technology to simulate progress_timer to write a new class // new_progress_timer to achieve output with any precision # include <iostream> # include <boost/progress. HPP> # include <boost/static_assert.hpp> // static assertions. The control precision ranges from 0 ~ Using namespace boost; using namespace STD; Template <int n = 2> class new_progress_timer: Public boost: timer {public: new_progress_timer (STD: ostream & OS = STD :: cout): m_ OS (OS) {boost_static_assert (N >=0 & n <= 10 );}~ New_progress_timer () // The Destructor is the core function. Save the IO stream status and set the output precision, {try {// Save the stream status STD: istream: fmtflags old_flags = m_ OS .setf (STD: istream: fixed, STD :: istream: floatfield); STD: streamsize old_prec = m_ OS .precision (n); // output time m_ OS <elapsed () <"s \ n" <STD :: endl; // restore the stream status m_ OS .flags (old_flags); m_ OS .precision (old_prec);} catch (...) {} // destructor must not throw an exception} PRIVATE: STD: ostream & m_ OS;}; // use the template to make a special case, the precision is 2 template <> class new_progress_timer <2>: Public boost: progress_timer {}; int main () {new_progress_timer <10> T; // The precision is 10 unsigned int I; int sum = 1; for (I = 0; I <1000000000; ++ I) // 1 billion command sum % = 2 ;}
Running result:
2.6560000000 s