Boost Timer Library (timer library)

xiaoxiao2021-03-06  67

Transfer from: http://www.c-view.org/tech/lib/cboost/timer.htm

Timer Library (timer library)

Translation: Tangtao English Documentation (English)

© Copyright 1999 Beman Dawes. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. The software described is provided "as is" without express or implied warranty, and with no claim as To its suitability for any purpose.

January 11, 2002

Summary

Timer library provides timer class -timer, timer time report class - progress_timer, and progress bar display class progress_display.

Header file Timer.HPP and Progress.hpp test program timer_test.cpp author Beman Dawes.

table of Contents

Timer class

Summary

Exception Safety

Future trend

Progress_timer class

Summary

Unusual security

Progress_display class

Summary

Unusual security

history

来 龙 去

Timer library has two headers and three classes:

Head File Function Timer.hpptimer Measurement Time ProGress.hppProgress_timer Measurement Time (Timer)

The design objects of the above three classes are quite clear - allowing programmers to easily call timing and progress reporting functions in debugging procedures or batch jobs. The interface specification of the Progress class has strong versatility, which can be replaced by other implementations for use in the GUI.

Timer class

Timer class measurement program consumes time. It is very helpful for some short-term timing tasks. The implementation here uses the CLOCK () function of the C standard library, so having a certain degree of portability, the cost of paying is - uncertain time accuracy. The maximum measurement time may be 596.5 hours (or less than this). Due to these restrictions, the TIMER class is not suitable for use in some high reliability, high-risk.

Summary

#include

Namespace boost {

Class timer {

PUBLIC:

Timer (); // Postcondition: Elapsed () == 0

// compiler generated copy constructor, Copy Assignment, and Dtor Apply

Void Restart (); // post: Elapsed () == 0

Double Elapsed () const; // return Elapsed Time in Seconds

Double ELAPSED_MAX () const; // return estimated maximum value for elapsed ()

// Portability Warning: ELAPSED_MAX () May Return Too High a value on systems // where std :: clock_t overflows or resets at surprising value.

Double ELAPSED_MIN () const; // return minimum value for elapsed ()

}; // Timer

} // Namespace Boost

Exception Safety

The constructor may throw std :: bad_alloc. Other member functions will not throw an exception.

Future trend

ED BREY has proposed a very reasonable request to provide a method to detect the maximum value of the ELAPSED () function returns, but such a detection function does not have portability. This problem has been made to the C language TIME extension function library workgroup, which will have a solution in the near future. Prior to this, the ELAPSED_MAX () function can be used, which returns an approximation.

Progress_timer class

Progress_timer automatic measurement program is time-consuming time, and displays time information when an object destructure is analyzed. The character information is output to std :: cout in the implementation of the offer.

The Progress_Timer class is often used in the statistical program. A simple example:

#include

int main ()

{

Progress_timer T; // Start Timing

// do something ...

Return 0;

}

It produces an appropriate output, such as:

1.23 s

Note: "s" is an abbreviation for the international standard measurement unit.

Summary

#include

Namespace boost {

Class Progress_timer: Public Timer, NoncopyAble {

PUBLIC:

PROGRESS_TIMER ();

Progress_timer (std :: ostream & os); // os is hint; importation may ignore

~ Progress_timer ();

}; // progress_display

} // Namespace Boost

Unusual security

The constructor may throw std :: bad_alloc. Other member functions will not throw an exception.

Progress_display class

The Progress_Display class displays a progress bar in an appropriate location. It is very suitable for use in people who need to understand the progress of the program.

For example, a BIG_MAP variable for a std :: Map <> type is required for a very long time calculation. The following code can display the entire calculation:

Progress_display show_progress (BIG_MAP.SIZE ());

FOR (BIG_MAP_T :: Iterator ITR = BIG_MAP: Begin ();

ITR! = BIG_MAP.END (); ITR)

{

// do the computation

...

Show_Progress;

}

When 70% of the data is processed, the entire progress is shown below:

0% 10 20 30 40 50 60 70 80 90 100%

| ---- | ---- | ---- | ---- | ---- | ---------------- |

********************************************

#include

Namespace boost {

Class Progress_Display: NoncopyAble {

PUBLIC:

PROGRESS_DISPLAY (unsigned long expected_count);

// Effects: restart (expected_count)

Progress_display (unsigned long expected_count,

Std :: Os Is Hint; Implementation May ignore

Const std :: string & S1 = "/ n", // Leading strings

Const std :: string & s2 = "",

Const std :: string & s3 = "")

// Effects: Save Copy of Leading Strings, Restart (Expected_count)

Void Restart (unsigned long expected_count);

// Effects: Display Appropriate Scale

// Postconditions: count () == 0, expected_count () == Expected_count

Unsigned long operator = (unsigned long increment)

// Effects: DISPLAY Appropriate Progress Tic if Needed.

// postconditions: count () == Original Count () increment

// Returns: count ().

Unsigned long operator ()

// Returns: Operator = (1).

Unsigned long count () const

// Returns: The Internal Count.

Unsigned long expected_count () const

// Returns: The expected_count from the constructor.

}; // progress_display

} // Namespace Boost

Unusual security

In addition to count () and expected_count (), other member functions have output actions, so an exception may be thrown theory. In practice, such an exception is generally unlikely, one but there is an abnormality may mean that there is more serious problems waiting for you. Note that there is no explicit destructor, so it won't throw an exception.

history

These some of the programmers used the old C and C code for many years. On the Boost's mailing list, Reid Sweatman recommends separating relatively universal Timer classes from a relatively dedicated Progress class. Sean Corfield recommends allowing output to any Ostream's derivation class. Dave Abrahams, Valentin Bonnard, Ed Brey, Andy Glew, and Dietmar KUHL provide useful comments. ED BREY recommends the Timer :: ELAPSED_MAX () function. John Maddock recommends Timer :: ELAPSED_MIN (). Toon Knapen recommends an optional heading string, which is used to mark progress display.来 龙 去

The early version declarations and implementations of the Timer class are separated. But this will bring trouble to those who are unwilling to build libraries. There is also difficulties when building DLLs (because libctions that may be correlated by different compilers). This causes clearly that you can use these classes but cannot be used. Therefore, the current implementation code is all modified into the inline form.

There have been several requests to achieve high performance Timer with special system APIs in different system platforms. John Maddock has submitted a version of the Win32 API. Test indicates that its precision is very high, but sometimes the reaction time is much slower than the std :: clock () function, which is too bad. In addition, this will result in a very dependent on operating system version (WinNT, Win95, etc.) and compiler (Microsoft and Borland compiler pass the test). Therefore, the std :: clock () function is more trustworthy than the unique Timer API of each platform.

© 2001-2002 C-View.org All Rights Reserved.

转载请注明原文地址:https://www.9cbs.com/read-89682.html

New Post(0)