doubleelapsed_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 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() { returndouble(1)/double(CLOCKS_PER_SEC); }
private: std::clock_t _start_time; }; // timer
而std::clock()是在C++中的ctime.h中定义的:
1
std::clock_tclock(void);
其中std::clock_t也定义在ctime.h中。
Defined in header typedef /* unspecified */ clock_t; Arithmetic type capable of representing the process running time of implementation-defined range and precision.
public: explicitprogress_timer( std::ostream & os = std::cout ) // os is hint; implementation may ignore, particularly in embedded systems : timer(), noncopyable(), m_os(os) {} ~progress_timer() { // A) Throwing an exception from a destructor is a Bad Thing. // B) The progress_timer destructor does output which may throw. // C) A progress_timer is usually not critical to the application. // Therefore, wrap the I/O in a try block, catch and ignore all exceptions. try { // use istream instead of ios_base to workaround GNU problem (Greg Chicares) std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed, std::istream::floatfield ); std::streamsize old_prec = m_os.precision( 2 ); m_os << elapsed() << " s\n"// "s" is System International d'Unites std << std::endl; m_os.flags( old_flags ); m_os.precision( old_prec ); } catch (...) {} // eat any exceptions } // ~progress_timer private: std::ostream & m_os;