#ifndef MYTIMER_ #define MYTIMER_ #include // Modified from Reviver's original source class MyTimer { public: MyTimer() : mstart(std::clock()){} // start counting time double GetTimeElapsed(){ clock_t total = std::clock()-mstart; // get elapsed time return double(total)/double(CLOCKS_PER_SEC); }; void restart() { mstart = std::clock(); } ~MyTimer(){}; private: std::clock_t mstart; }; inline std::ostream& operator<<(std::ostream& o, MyTimer& mt){ long s = static_cast (mt.GetTimeElapsed ()); long m = 0; if(s > 60) { m = s / 60; s = s - (m*60); o << m << " min " << s << " sec "; } else { o << mt.GetTimeElapsed () << " sec "; } return o; } // Modified from boost library class MyTimer_display { public: explicit MyTimer_display( unsigned long expected_count, std::ostream & os = std::cout ) // os is hint; implementation may ignore : _os(os) { restart(expected_count); } void restart( unsigned long expected_count ) // Effects: display appropriate scale // Postconditions: count()==0, expected_count()==expected_count { _count = _next_tic_count = _tic = 0; _expected_count = expected_count; _os << "\n0% 10 20 30 40 50 60 70 80 90 100%\n" "|----|----|----|----|----|----|----|----|----|----|" << std::endl; if ( !_expected_count ) _expected_count = 1; // prevent divide by zero } // restart unsigned long operator+=( unsigned long increment ) // Effects: Display appropriate progress tic if needed. // Postconditions: count()== original count() + increment // Returns: count(). { if ( (_count += increment) >= _next_tic_count ) { display_tic(); } return _count; } unsigned long operator++() { return operator+=( 1 ); } unsigned long count() const { return _count; } unsigned long expected_count() const { return _expected_count; } private: std::ostream & _os; // may not be present in all imps unsigned long _count, _expected_count, _next_tic_count; unsigned int _tic; void display_tic() { // use of floating point ensures that both large and small counts // work correctly. static_cast<>() is also used several places // to suppress spurious compiler warnings. unsigned int tics_needed = static_cast( (static_cast(_count)/_expected_count)*50.0 ); do { _os << '*' << std::flush; } while ( ++_tic < tics_needed ); _next_tic_count = static_cast((_tic/50.0)*_expected_count); if ( _count == _expected_count ) { if ( _tic < 51 ) _os << '*'; _os << std::endl; } } // display_tic }; #endif