Boost-timer Library

Source: Internet
Author: User
Tags setf

The timer library contains three components: Timer class, progress_timer class, And progress_display (progress indication class ).

Timer:

# Include <boost/Timer. HPP>

 1 # Include <iostream>
2 # Include " ../Boost_000048_0/Boost/Timer. HPP "
3 Using Namespace STD;
4
5 Int Main ()
6 {
7 Boost: timer T;
8 Cout <t. elapsed_max ()/ 3600 < " Hours " <Endl;
9 Cout <t. elapsed_min () < " Seconds " <Endl;
10 Cout <" Now print elapsed time: " <T. elapsed () < " Seconds " <Endl;
11
12 Return 0 ;
13 }

Source code:

 1   Class Timer
2 {
3 Public :
4 Timer () {_ start_time = STD: Clock ();} // Postcondition: elapsed () = 0
5 // Timer (const timer & SRC ); // Post: elapsed () = SRC. elapsed ()
6 // ~ Timer (){}
7 // Timer & operator = (const timer & SRC ); // Post: elapsed () = SRC. elapsed ()
8 Void Restart () {_ start_time = STD: Clock ();} // Post: elapsed () = 0
9 Double Elapsed () Const // Return elapsed time in seconds
10 { Return Double (STD: Clock ()-_ start_time)/clocks_per_sec ;}
11
12 Double Elapsed_max () Const // Return estimated maximum value for elapsed ()
13 // Portability warning: elapsed_max () may return too high a value on Systems
14 // Where STD: clock_t overflows or resets at surprising values.
15 {
16 Return ( Double (STD: numeric_limits <STD: clock_t >:: max )())
17 - Double (_ Start_time ))/ Double (Clocks_per_sec );
18 }
19
20 Double Elapsed_min () Const // Return minimum value for elapsed ()
21 { Return Double ( 1 )/ Double (Clocks_per_sec );}
22
23 Private :
24 STD: clock_t _ start_time;
25 }; // Timer

Timer is not suitable for high-precision timing. Its accuracy depends on OS and compiler, and cannot be expanded to the platform. It is also not suitable for the measurement of Large-span time periods. It can provide a maximum time span of several hundred hours. If you want to time by day, month, or year, use the boost: date_time database instead of timer.

Progress_timer Library:

Inherited from the timer, the automatic output time saves the time required for timer to manually call elapsed (). Of course, you can also call the elapsed () function to output the time.

ToProgramYou can use {} to specify the lifetime of progress_timer.

 1 # Include <iostream>
2 # Include " ../Boost_1_48_0/Boost/progress. HPP "
3 Using Namespace STD;
4
5 Int Main ()
6 {
7 {
8 Boost: progress_timer T;
9 Int TMP = 0 ;
10 For ( Int I = 0 ; I < 100000000 ; ++ I)
11 {
12 TMP = I;
13 }
14 }
15
16 {
17 Boost: progress_timer T;
18 Int TMP = 5 ;
19 For ( Int I = 0 ; I < 100000000 ; ++ I)
20 {
21 TMP * = I;
22 TMP = 5 ;
23 }
24 }
25 Return 0 ;
26 }

The progress_timer class is defined as follows:

1 ClassProgress_timer:PublicTimer, noncopyable
2{
3 Public:
4ExplicitProgress_timer ();
5Progress_timer (STD: ostream & OS );
6~ Progress_timer ();
7};

The parameter of the second constructor is a stream. The default value is STD: cout. you can replace it with other streams to output time to other streams, such as ofstream and ostringstream.

The output precision of progress_timer is only two digits after the decimal point, that is, 1% seconds. Therefore, you can create a progress_timer class.

 1 # Include <iostream>
2 # Include" ../Boost_1_48_0/Boost/progress. HPP "
3 # Include " ../Boost_000048_0/Boost/static_assert.hpp "
4 Using Namespace STD;
5
6 Template < Int N = 2 >
7 Class New_progress_timer: Public Boost: timer, boost: noncopyable
8 {
9 Public :
10 Explicit New_progress_timer (STD: ostream & OS = STD: cout): m_ OS (OS)
11 {
12 Boost_static_assert (n> = 0 & Amp; n <= 10 );
13 }
14
15 ~ New_progress_timer ()
16 {
17 Try
18 {
19 STD: istream: fmtflags old_flags =
20 M_ OS .setf (STD: istream ::Fixed , STD: istream: floatfield );
21 STD: streamsize old_prec = m_ OS .precision (N );
22 M_ OS <elapsed () < " S \ n " <STD: Endl;
23 M_ OS .setf (old_flags );
24 M_ OS .precision (old_prec );
25 }
26 Catch (...)
27 {}
28 }
29
30 Private :
31 // The stream cannot be copied.
32 STD: ostream & m_ OS;
33 };
34
35 Template <>
36 Class New_progress_timer < 2 >: Public Boost: progress_timer
37 {};
38
39
40 Int Main ()
41 {
42 {
43 Boost: progress_timer T;
44 Int TMP = 0 ;
45 For ( Int I = 0 ; I < 100000000 ; ++ I)
46 {
47 TMP = I;
48 }
49 }
50
51 {
52 Boost: progress_timer T;
53 Int TMP = 5 ;
54 For ( Int I = 0 ; I < 100000000 ; ++ I)
55 {
56 TMP * = I;
57 TMP = 5 ;
58 }
59 }
60
61 {
62 New_progress_timer < 10 > T (STD: cout );
63 Int TMP = 5 ;
64 For ( Int I = 0 ; I < 100000000 ; ++ I)
65 {
66 TMP * = I;
67 TMP = 5 ;
68 }
69 }
70
71 Return 0 ;
72 }

 

Progress_display:

 1   Int Main ()
2 {
3 STD: vector <STD :: String > V ( 1000000 );
4 Boost: progress_display Pd (V. Size ());
5 STD: ofstream OFS;
6 OFS. Open ( " Test.txt " );
7 If (! OFS. is_open ())
8 {
9 Return - 1 ;
10 }
11
12 For ( Int I = 0 ; I <v. Size (); ++ I)
13 {
14 OFS <I <STD: Endl;
15 ++ PD;
16 }
17
18 OFS. Close ();
19
20 Return 0 ;
21 }

 

Date_time is too complicated .... Pass ~~~ : D

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.