Boost source code brief analysis series - Timer (1)
"Boost library is a C library that passes through thousands of hammer, portable, providing source code, as a standard library, is one of the engines of the C standardization process. Boost library initiated by C Standard Board Working Group, Impact in C Community Very large, its members have nearly 2,000 people. The Boost library has brought us the latest, coolest, most practical technology, which is a "quasi" standard library that is not buckled. "
Excribble from "C Boost Introduction".
"It has been circulating such a saying, I want to be a master, be sure to read the source code. Which code is a good material? C standard library source code? No, if you read, you will find: either all kinds Realizing the unique expression makes people feel that the horror code style (such as going to underline) is uncomfortable. The code of the Boost library is quite clear, the annotation is reasonable, naming norms, absolutely a model for reading. "
Excerpt from "walking into boost" - Ah
BOOST has been supplemented as a C standard library, which is established on the basis of STL. There are many people on the Internet to use, especially for learners who wish to study C source code provide excellent textbooks. This is because the Boost library is open source, and its source code is simple, the format specification, the code itself is clear, plus the detailed explanation document, has achieved the attraction of the Boost library to beginners .
This is some of my awareness I am in touch with Boost, and we start from Timer ...
First, you should read the source code should first browse the Boost documentation, in which the components of the TIMER library are browsed, and the aspects of the application should be noted.
Document above description:
Timer consists of two header files, including three classes. It can be seen that it is very simple from the complexity of class, which is the reason I first choose to read him. In fact, from our later source code, it can be seen that there is inheritance between class Timer and Progress_Timer, and class Progress_Display is independently seek.
Head file class Description Timer.hpp Timer Measurement Time Progress.hpp Progress_timer uses Timer's object measurement time, and display time progress.hpp progress_display when the object destructure is displayed, showing a time schedule
Class Timer
The main task of class Timer is to measure the time of consumption, but he generally is only suitable for the task of time consuming, because the maximum metering time of the Timer object is limited to 596.5 hours. Moreover, since the system time of the function clock () in the C standard library is used, the accuracy of time meter is limited by the system platform, and the different platforms may have large access by the same program measurement time.
This source code is very short:
#ifndef boost_timer_hpp
#define boost_timer_HPP
#include
#include
#include
# i iDef boost_no_stdc_namespace
Namespace std {using :: clock_t; using :: clock;
# Endif
Namespace boost {
Class Timer
{
PUBLIC:
Timer () {_Start_time = std :: clock ();}
Void restart () {_start_time = std :: clock (); Double Elapsed () Const
{RETURN DOUBLE (std :: clock () - _Start_time) / clocks_per_sec;}
Double ELAPSED_MAX () Const
{
Return (Double (std :: numeric_limits
- double (_start_time)) / double (clocks_per_sec);
}
Double Elapsed_min () Const
{RETURN DOUBLE (1) / double (clocks_per_sec);}
Private:
Std :: clock_t _start_time;
}
}
#ENDIF
Although the code is not long, I still have a habitual browsing of the entire class declaration section, the code segment first is the three header files in INCLUDE, one is
among them
#ifndef boost_timer_hpp
#define boost_timer_HPP
// ...
#ENDIF
It is the traditional technology that defines the header file, called the container, which prevents the same header file from being included multiple times. When the header file is first included, boost_timer_hpp is defined to assign a value, and the content is also read. When this header is included, the compiler will find that Boost_tiMer_HPP has been assigned, ignoring the content.
# i iDef boost_no_stdc_namespace
Namespace std {using :: clock_t; using :: clock;
# Endif
The same contains the protective comparative technology here, there is a two types of Clock_T and Clock defined in the STD name space.
Then the code below is the definition of the class. For a class's code, the code is recognized from her inheritance relationship. This class does not have a base class, so skipping, starting from the user interface provided, that is, according to the instruction document speculation The constructor provided and the functionality to the user's member function:
PUBLIC's function in Timer
Constructor: Timer () // It doesn't have to say it.
Member function: restart () // is probably a re-count.
ELAPSED () // should be a time that has passed.
ELAPSED_MAX () // gives this class to handle the maximum time.
ELAPSED_MIN () // gives the minimum time that can be processed.
Data members: std :: clock_t _start_time; // initialized the time at the time when the Timer object was built.
This is all functions, because these functions are simple, there is no need to introduce one here, given a description:
1. The clock () function is a function in the C standard library and returns the current time of the system. 2. Clocks_per_sec A macro, is defined in
3. std :: numeric_limits
Here is the main content of this class, we see this class does not use virtual functions.
Now we can simply test the time of the code execution of the object of the Timer class. It is used to prove that this type of measurement time has different results as the system status; has different results with different compilers.
#include
#include
Using namespace std;
USING NAMESPACE BOOST;
Int main (void)
{
INT i = 0;
Timer testtm;
Testtm.restart ();
{
For (; i <1000; i )
{
For (int J = 0; j <10000; j )
{
J ;
J -;
}
}
}
Cout << testtm.elapsed () << endl;
System ("pause");
Return 0;
}
I have compiled with VC6.0 and DEV-C , respectively, respectively, as follows:
Test number vc6.0 DEV-C 1 1.64 0.89 2 1.734 0.984 3 1.756 5 1.64 0.937 6 1.718 0.968 7 1.75 0.921 8 1.609 0.921 9 1.718 0.89 10 1.734 1.078 average 1.693 0.945
As can be seen from this table, the results obtained by different compilers are significantly different (gaps), and the same compiler is compiled, each running result is different (the gap is relatively small).
It is not the advantages and disadvantages of the two compilers, nor does it get evidence of any compiler.
Introduce other two classes of Timer in the next "Boost Source Code" - Timer (2) ", and related routines.