The timer is a small library that provides easy time measurement and progress display, including timer, Progress_timer, and progress_display three classes. Not suitable for high-precision time measurement tasks, precision depends on the operating system or compiler, the maximum time span available is only hundreds of hours, and is not suitable for large-span time-period measurements.
Timer class
First look at how the standard library is timed:
#include <iostream> #include <ctime>using namespace Std;int main () {clock_t begin, End;begin = Clock (); for ( Long i=0; i<1000000; ++i); end = Clock (); cout << "Loop time is:" << (Double) (end-begin)/clocks_per_sec << Endl;system ("Pause ");}
The clock () in the standard library <ctime> header file is used, which returns the number of clock since the start of the process, and the number of clock per second is defined by the macro clocks_per_sec. Under Win32 is 1000, under Linux is 1000000. In other words, the precision under Win32 is millisecond, and the precision under Linux is subtle.
Then look at the timer class source
Boost TIMER.HPP header file---------------------------------------------////Copyright beman Dawes 1994-99. Distributed under the boost//software License, Version 1.0. (see accompanying file//license_1_0.txt or copy at Http://www.boost.org/LICENSE_1_0.txt)//See http://www.boost.org/li Bs/timer for documentation.//Revision history//April Modified to use new <boost/limits.hpp> header. (Jmaddock)//implementation to without library//builds. See docs for more rationale. (Beman Dawes)//Sep Elapsed_max () and Elapsed_min () added (John maddock)//Jul Second beta//6 Jul 99 Initial boost Version#ifndef boost_timer_hpp#define boost_timer_hpp#include <boost/config.hpp> #include < ctime> #include <boost/limits.hpp># ifdef boost_no_stdc_namespace NAMESPACE std {using:: clock_t; using:: Cloc K }# endifnamespace Boost {//timer-------------------------------------------------------------------////A Timer object measures elapsed time.//It's recommended that implementations measure wall clock rat Her than cpu//time since the intended use was performance measurement on systems where//total elapsed time was more impo Rtant than just process or CPU time.//warnings:the maximum measurable elapsed time may well is only 596.5+ hours//due to implementation limitations. The accuracy of timings depends on the//accuracy of timing information provided by the underlying platform, and//this Varies a great deal from platform to Platform.class timer{Public:timer () {_start_time = Std::clock ();}//Post condition:elapsed () ==0//timer (const timer& SRC); Post:elapsed () ==src.elapsed ()//~timer () {}//timer& operator= (const timer& SRC); Post:elapsed () ==src.elapsed () void restart () {_start_time = Std::clock ();}//Post:elapsed () ==0 double elapsed ( ) const//return elapsed time in seconds {return double (Std::clock ()-_start_time)/clocks_per_sec;} Double Elapsed_max () const//return estimated maximum value for elapsed ()//Portability Warning:elapsed_max () may re Turn too high a value in systems//where std::clock_t overflows or resets at surprising values. {return (double (Std::numeric_limits<std::clock_t>::max) ())-double (_start_time))/double (clocks_per_sec ); } double Elapsed_min () const//return minimum value for elapsed () {return double (1)/double (CLOCKS_PER_SEC) ; } private:std::clock_t _start_time;}; Timer}//namespace BOOST#ENDIF//BOOST_TIMER_HPP
The timer also uses the clock () in the standard library <ctime> header file, and once the timer object is declared, the constructor starts the timing and can then be used to measure the elapsed time since creation to elapsed () was called by elapsed. Such as
#include <boost/timer.hpp>//header file # include <iostream>using namespace boost; Namespace using namespace Std;int Main () {timer T; Once the timer object is created, its constructor starts the timed work for (int i=0; i<1000; ++i) for (int j=0; j<1000; ++j); cout << t.elapsed () < < "s" << Endl; Call elapsed () to measure the elapsed time since it was created system ("pause");
Progress_timer class Progress_timer inherit from the timer, can be used as a timer to use the Progress_timer. Progress_timer is simpler, just declare the Progress_timer object, and automatically output the elapsed time when it is refactored.
#include <boost/progress.hpp> #include <iostream>using namespace boost; Using namespace std;void func () {Progress_timer pt;for (long i=0; i<1000000; ++i);} int main () {func (); System ("Pause");}
Progress_display class Progress_display can display program execution progress on the console, its constructor accepts a long parameter as the cardinality of the progress display, then displays a progress bar interface, the overloaded operator + = and + + are used to increase the cardinality, The member count () can return the current count.
#include <boost/progress.hpp> #include <iostream>using namespace boost; Using namespace std;void func () {progress_display pd (10000000); for (int i=0; i<10000000; ++i) {++pd;}} int main () {func (); System ("Pause");}
Boost Library Learning Timer Library