Timer: C++11

This commit is contained in:
Paul Beckingham 2016-02-03 20:14:17 -05:00
parent 8577449960
commit f59e15185f
2 changed files with 9 additions and 22 deletions

View file

@ -34,11 +34,7 @@
extern Context context; extern Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Timer starts when the object is constructed.
Timer::Timer () Timer::Timer ()
: _description ("-")
, _running (false)
, _total (0)
{ {
} }
@ -46,8 +42,6 @@ Timer::Timer ()
// Timer starts when the object is constructed with a description. // Timer starts when the object is constructed with a description.
Timer::Timer (const std::string& description) Timer::Timer (const std::string& description)
: _description (description) : _description (description)
, _running (false)
, _total (0)
{ {
start (); start ();
} }
@ -59,14 +53,11 @@ Timer::~Timer ()
stop (); stop ();
std::stringstream s; std::stringstream s;
s << "Timer " // No i18n s << "Timer "
<< _description << _description
<< " " << " "
<< std::setprecision (6) << std::setprecision (6)
#ifndef HAIKU
// Haiku fails on this - don't know why.
<< std::fixed << std::fixed
#endif
<< _total / 1000000.0 << _total / 1000000.0
<< " sec"; << " sec";
@ -78,7 +69,7 @@ void Timer::start ()
{ {
if (!_running) if (!_running)
{ {
gettimeofday (&_start, NULL); gettimeofday (&_start, nullptr);
_running = true; _running = true;
} }
} }
@ -89,7 +80,7 @@ void Timer::stop ()
if (_running) if (_running)
{ {
struct timeval end; struct timeval end;
gettimeofday (&end, NULL); gettimeofday (&end, nullptr);
_running = false; _running = false;
_total += (end.tv_sec - _start.tv_sec) * 1000000 _total += (end.tv_sec - _start.tv_sec) * 1000000
+ (end.tv_usec - _start.tv_usec); + (end.tv_usec - _start.tv_usec);
@ -115,7 +106,7 @@ void Timer::subtract (unsigned long value)
unsigned long Timer::now () unsigned long Timer::now ()
{ {
struct timeval now; struct timeval now;
gettimeofday (&now, NULL); gettimeofday (&now, nullptr);
return now.tv_sec * 1000000 + now.tv_usec; return now.tv_sec * 1000000 + now.tv_usec;
} }

View file

@ -35,10 +35,8 @@ class Timer
{ {
public: public:
Timer (); Timer ();
Timer (const std::string&); explicit Timer (const std::string&);
~Timer (); ~Timer ();
Timer (const Timer&);
Timer& operator= (const Timer&);
void start (); void start ();
void stop (); void stop ();
@ -48,12 +46,10 @@ public:
static unsigned long now (); static unsigned long now ();
private: private:
std::string _description; std::string _description {"-"};
bool _running; bool _running {false};
struct timeval _start; struct timeval _start {};
unsigned long _total; unsigned long _total {0};
}; };
#endif #endif
////////////////////////////////////////////////////////////////////////////////