boost--time and Date--(1) Introduction of Timer Library

Source: Internet
Author: User
Tags time and date

(a) A brief introduction to the timer library
A timer is a small library that provides simple time measurement and progress display capabilities, and can be used for timing tasks such as performance testing. The Timer library contains three components: a timer class, a timer, a progress_timer, and a progress indicator class Progress_display.

(ii) Timer class
The Timer class can measure the passage of time and is a small timer that provides millisecond-level timing accuracy and manipulation functions. It is located under the Boost namespace. You need to include a header file when you use:

  Include <boost/timer.hpp>

(1) Use of timer

#include <boost/timer.hpp>#include<iostream>intMainintargcChar*argv[])    {Boost::timer T; //gets the maximum time precision that a timer can representstd::cout<<"Max TimeSpan:"<<t.elapsed_max ()/3600<<"h"<<Std::endl; //Gets the minimum time precision that a timer can representstd::cout<<"min TimeSpan:"<<t.elapsed_min () <<"s"<<Std::endl; Std::cout<<"Now time elapsed:"<<t.elapsed () <<"s"<<Std::endl; return 0;}

Once the timer object is declared, its constructor starts the timing work, and the elapsed () function can then be called to get the interval from the object creation to the elapsed () function call.

(2) The source of the timer

//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 atHttp://www.boost.org/LICENSE_1_0.txt)// SeeHttp://www.boost.org/libs/timerFor documentation.//Revision History//APR Modified to use new <boost/limits.hpp> header. (Jmaddock)//Implementation to-to-allow use without library//builds. See docs for more rationale. (Beman Dawes)//Sep Elapsed_max () and Elapsed_min () added (John maddock)//Jul Second Beta//6 Jul Initial boost version#ifndef boost_timer_hpp#defineBoost_timer_hpp#include<boost/config.hpp>#include<ctime>#include<boost/limits.hpp># ifdef Boost_no_stdc_namespacenamespaceSTD {using:: clock_t;using:: Clock;} # endifnamespaceBoost {//Timer-------------------------------------------------------------------////A Timer Object measures elapsed time.//It's recommended that implementations measure wall clock rather than CPU//Time since the intended use are performance measurement on systems where//Total elapsed time was more important 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.classtimer{ Public: Timer () {_start_time= Std::clock (); }//postcondition:elapsed () ==0//timer (const timer& SRC); //post:elapsed () ==src.elapsed ( )//~timer () {}//timer& operator= (const timer& SRC); //post:elapsed () ==src.elapsed ( )  voidRestart () {_start_time = Std::clock ();}//post:elapsed () ==0  DoubleElapsed ()Const                  //return elapsed time in seconds{return  Double(Std::clock ()-_start_time)/clocks_per_sec;} DoubleElapsed_max ()Const   //return estimated maximum value for elapsed ()//Portability Warning:elapsed_max () may return too + a value on 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); }  DoubleElapsed_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

  Note:the timer used the CLOCKS_PER_SEC macro, the macro in the Win32 value is 1000, and the value of Linux is 1000000, that is, it is specific platform or compiler-related, so it is not suitable for cross-platform use, It is also not suitable for the measurement of large span time.

(ii) Progress_timer class
Progress_timer inherits from the timer, which automatically outputs time when it is refactored. In the Boost namespace, include header files when used:#include <boost/progress.hpp>. Other uses are similar to the timer.

If you want to measure a piece of code run time in your program, you can include this code with {} and a line of code in {} to define a the Progress_timer object.

{    // auto-output time interval (from object creation to destruction)    //dosomethingat destructor ...}

(iii) Progress_display class
Progress_display can display the progress of program execution on the console. In the Boost namespace, add header files when used:#include <boost/progress.hpp>.

It is a standalone class that has no contact with the timer and Progress_timer.

#include <iostream><boost/progress.hpp>int main (int argc,Char * argv[]) {    // declaration base size is    boost::p rogress_display PD (+);      for (int i=0;i<; i++) {        + +pd;    }         return 0 ;}

boost--time and Date--(1) Introduction of Timer Library

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.