Use the progress_timer component to extend the timing accuracy

Source: Internet
Author: User

 

// 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.