Start today to see the full development of the Boost Library guide: in-depth C + + "Quasi" Standard library (3rd edition) of the book. I use it boost 1.57
, and the book uses the left and right boost 1.43
. Originally thought that the gap is not big, the result in the first timer
library when encountered the problem.
The book says direct use of the boost::timer
class. But when I look at the source code, I find that timer
it is now just a namespace instead of a class, the real class is cpu_timer
and auto_cpu_timer
, at the same time, there is no elapsed_max
elapsed_min
way out. MORE: is_stopped
, start
, resume
and format
methods.
In the elapsed
method, the returned is no longer a number, but one struct cpu_times
, in this struct, defined as:
struct cpu_times{ nanosecond_type wall; nanosecond_type user; nanosecond_type system; void0LL; }};
According boost
to the official document, wall
refers to the actual time the program is running, user
referring to the user CPU time, referring to the system
system CPU time. Among them, the real time is susceptible to other program operation interference, is unstable. If it's a measure of how long the algorithm runs, the better metric is the sum user
system
. As can be seen from the variable type, the time unit is nanosecond (NS).
The default output format is:
5.713010s Wall, 5.709637s user + 0.000000s system = 5.709637s CPU (99.9%)
The documentation is interpreted as:
In other words, this program ran in 5.713010 seconds as would is measured by a clock on the wall, the operating system cha Rged it for 5.709637 seconds of the user CPU time and 0 seconds of the system CPU time, the total of these is 5.709637, and T Hat represented 99.9 percent of the wall clock time.
The format can be customized, and the default format is defined as:
" %ws wall, %us user + %ss system = %ts CPU (%p%)\n"
The meaning is:
Sequence |
Replacement Value |
%w |
Times.wall |
%u |
Times.user |
%s |
Times.system |
%t |
Times.user + Times.system |
%p |
The percentage of Times.wall represented by Times.user + Times.system |
When auto_cpu_timer
defined, the format content can be passed into the constructor to control the output format.
For cpu_timer
classes, there is the Format function, defined as:
std::string format(shortconststd::string& format);std::string format(short places = default_places);
Where places controls the number of decimal places of the time, format is the just format string.
The main content is these, now look at a small example:
#include <iostream>#include <boost/timer/timer.hpp>#include <cmath>using namespace STD;using namespaceBoostintMain () {Timer::cpu_timer T; Timer::auto_cpu_timer Auto_timer (6,"%ws Real time\n");intA for(Longi =0; I <100000000; ++i) A =sqrt(i * i);//Spend some time cout<<"is started:"<< (t.is_stopped)?"No":"Yes") << Endl;cout<< T.format (2,"%us user +%ss system" "=%ts (%p%)") << Endl;return 0;}
It is important to note that the compile-time needs to link the boost
relevant library, rather than the previous can run directly:
g++ timer.-o-lboost_timer-lboost_system
The output is:
is started: yes2.080.002.08s(99.6%)2.088596realtime
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Boost Timer Learning Notes