// Timer.cpp // definition of a stopwatch class used to time code // declaration: Timer Watch; construct a watch set at 0.00 seconds // methods: void Watch.Start(); starts the watch // void Watch.Stop(); stops the watch // double Watch.Time(); returns the current elapsed time // void Watch.Reset(); reset the elapsed time to 0.0 seconds // tom bailey 13 feb 98 #include "timer.h" Timer::Timer(): elapsedTime( 0.0 ), running( false ) { } Timer::~Timer() { } void Timer::start() { if( !running ) { startTime = clock(); running = true; } } void Timer::stop() { clock_t stopTime; if( running ) { stopTime = clock(); running = false; elapsedTime += difference( stopTime, startTime ); } } double Timer::time() { if( running ) { stop(); start(); } return elapsedTime; } void Timer::reset() { if( running ) { stop(); elapsedTime = 0.0; start(); } else elapsedTime = 0.0; } double Timer::difference( clock_t stopTime, clock_t startTime ) { return double( stopTime - startTime ) / double( CLK_TCK ); }