Recently, I’ve been reading the Boost code and writing some analyses on the usage and implementation of useful modules in the Boost libraries, with updates to come periodically.
Note: When using Boost, don’t be lazy and directly use using namespace std;
, as there are many name collisions with the standard library, and the introduction of namespace
is precisely to solve this problem. Reasonable and correct use of namespaces is what a qualified cpp programmer should do.
Timer
The current version of Boost (1.62) includes two versions of timer: one is timer
(v1), which is implemented using C/C++ library functions and provides low precision (relying on the operating system or compiler); the timer does not require linking libraries, just include <boost/timer.hpp>
; the second is cpu_timer
(v2), which uses the API of the operating system based on the chrono
library, offering higher precision timing.
timer
1 |
|
The timer in Boost is implemented by calling the C/C++ library function clock()
:
1 | class timer |
And std::clock()
is defined in ctime.h
in C++:
1 | std::clock_t clock(void); |
Where std::clock_t
is also defined in ctime.h
.
Defined in header
typedef /* unspecified */ clock_t;
Arithmetic type capable of representing the process running time of implementation-defined range and precision.
In fact, the above code calling timer is equivalent to:
1 |
|
Note: The number of clock ticks per second is defined by the macro CLOCKS_PER_SEC
, which varies across different operating systems. On Win32, it’s 1,000 (with a timing precision of 1s/1,000=1ms), while on Linux, it’s 1,000,000 (with a timing precision of 1s/1,000,000=1μs).
progress_timer
progress_timer
inherits from the timer
class, thus having all member functions of the timer class (the interface is the same as timer). We can perform any operation on a progress_timer
object that can be done on a timer
object.
The constructor of progress_timer
consists of the constructor of timer
+ the constructor defined in progress_timer
. When constructing progress_timer
, we need to pass an IO stream object to output the time to that stream upon destruction. The default is std::cout
, but other standard output streams (ofstream/ostringstream) can be used alternatively, or the output of cout can be redirected using cout.rdbuf().
1 | public: |
As can be seen, when we construct a progress_timer
, the timing starts automatically (timer::timer()
), and when the progress_timer
is destructed, it outputs the time elapsed from construction to destruction.
progress_display
progress_display
can show the execution progress of the program in the console, but I think this class is pretty useless, so I won’t elaborate…
date_time
Boost has a date_time library to handle time, and date_time needs to be compiled to use, requiring linking to the boost_date_time
library during compilation. The date_time consists of two parts: gregorian
for handling dates and posix_time
for handling time.
More about the interfaces of date_time in Boost can be found here: Chapter 10. Boost.Date_Time
date_time basically covers our needs for calculating between dates, but the date in the date_time library is based on the Gregorian calendar, supporting only dates from 1400-01-01 to 9999-12-31.
Without further ado, let’s look at some simple and common usages… More usages can be found in the Boost documentation or “The Complete Development of the Boost Library”.
1 |
|
Some section code of the date class in date_time:
For more details, see boostcode/date_time/date.hpp
1 | template<class T, class calendar, class duration_type_> |