//------------------------------------------------------------------------------ // File : stopwatch.cpp //------------------------------------------------------------------------------ // GLVU : Copyright 1997 - 2002 // The University of North Carolina at Chapel Hill //------------------------------------------------------------------------------ // Permission to use, copy, modify, distribute and sell this software and its // documentation for any purpose is hereby granted without fee, provided that // the above copyright notice appear in all copies and that both that copyright // notice and this permission notice appear in supporting documentation. // Binaries may be compiled with this software without any royalties or // restrictions. // // The University of North Carolina at Chapel Hill makes no representations // about the suitability of this software for any purpose. It is provided // "as is" without express or implied warranty. //------------------------------------------------------------------------------ // Stopwatch -- a cross platform performance timing class // $Id: stopwatch.cpp,v 1.1.1.1 2003/02/23 22:05:32 harrism Exp $ //------------------------------------------------------------------------------ #include // for sprintf on some platforms #include "stopwatch.hpp" #include #ifdef _WIN32 # define WIN32_LEAN_AND_MEAN # include #endif //------------------------------------------------------------------------------ // Base Class //------------------------------------------------------------------------------ static char baseType[] = "Stopwatch virtual base class"; /** * Constructor. Stopwatches are stopped at construction time. * Call Start() to make it start ticking. * @param name A name to set on the timer. If no name is supplied the name * "Stopwatch" will be used. * @see SetName */ StopwatchBase::StopwatchBase(const char *name) : elapsedTime(0.0f), numStarts(0), running(false), sw_type(baseType) { if (name==0) name = "Stopwatch"; SetName(name); } /** * Destructor. */ StopwatchBase::~StopwatchBase() { } #if 0 void StopwatchBase::Start() { if (!running) { markTime(); running = true; } numStarts++; } void StopwatchBase::Stop() { if (running) { elapsedTime += diffTime(); running = false; } } void StopwatchBase::Reset() { if (running) { markTime(); numStarts=1; } else numStarts = 0; elapsedTime = 0.0f; } float StopwatchBase::GetTime() const { if (running) return (elapsedTime + diffTime()); else return (elapsedTime); } #endif /** * Return the average elapsed time over a number of starts. * Equals the total elapsed time over the number of starts since the last * Reset() or since construction. * * @warning Be sure to stop the stopwatch with Stop() before calling this. * Otherwise the results will be meaningless. */ float StopwatchBase::GetAvgTime() const { if (!numStarts) return 0; // If they call GetAvgTime while the clock is running there's // no telling what they'll get since they'll be averaging in one // incomplete period if (running) return (elapsedTime+diffTime()/numStarts); else return elapsedTime/numStarts; } /** * Return the number of calls to Start() since construction or * since the last Reset() */ int StopwatchBase::GetNumStarts() const { return numStarts; } /** * Sets a name on the stopwatch that can be used to identify the * timer for debugging printout purposes. Used in the output format * defined by the ostream << operator. * @see SetName(int) */ void StopwatchBase::SetName(const char *s) { // Copy s into our name buffer char *d = sw_name; while ((*d++ = *s++) && ((d-sw_name)