// @(#)root/base:$Name: $:$Id: Stopwatch.h,v 1.2 2008/02/25 13:53:06 ppost Exp $ // Author: Fons Rademakers 11/10/95 /************************************************************************* * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * * All rights reserved. * * * * For the licensing terms see $ROOTSYS/LICENSE. * * For the list of contributors see $ROOTSYS/README/CREDITS. * *************************************************************************/ #ifndef _Stopwatch #define _Stopwatch ////////////////////////////////////////////////////////////////////////// // // // TStopwatch // // // // Stopwatch class. This class returns the real and cpu time between // // the start and stop events. // // // ////////////////////////////////////////////////////////////////////////// class Stopwatch { private: enum EState { kUndefined, kStopped, kRunning }; double fStartRealTime; //wall clock start time double fStopRealTime; //wall clock stop time double fStartCpuTime; //cpu start time double fStopCpuTime; //cpu stop time double fTotalCpuTime; //total cpu time double fTotalRealTime; //total real time EState fState; //stopwatch state int fCounter; //number of times the stopwatch was started inline static double GetRealTime(); inline static double GetCPUTime(); public: inline Stopwatch(); inline void Start(int reset = 1); inline void Stop(); inline void Continue(); inline int Counter() const { return fCounter; } inline double RealTime(); inline void Reset() { ResetCpuTime(); ResetRealTime(); } inline void ResetCpuTime(double time = 0) { Stop(); fTotalCpuTime = time; } inline void ResetRealTime(double time = 0) { Stop(); fTotalRealTime = time; } inline double CpuTime(); }; //______________________________________________________________________________ #ifndef _WIN32 #define R__UNIX #endif #if defined(R__UNIX) # include # include # include static double gTicks = 0; #elif defined(R__VMS) # include # include static double gTicks = 1000; #elif defined(_WIN32) # include # include # include # undef min // unfortunately, defines min/max as macros # undef max //# include "TError.h" const double gTicks = 1.0e-7; static __int64 gTicksQPC = -1; // < 0 means "not yet initialized" //# include "Windows4Root.h" #endif inline Stopwatch::Stopwatch() { // Create a stopwatch and start it. #ifdef R__UNIX if (!gTicks) gTicks = (double)sysconf(_SC_CLK_TCK); #endif #ifdef _WIN32 if( gTicksQPC < 0 ) { LARGE_INTEGER freq; QueryPerformanceFrequency( &freq ); gTicksQPC = (double)freq.QuadPart; } #endif Start(); } //______________________________________________________________________________ inline void Stopwatch::Start(int reset) { // Start the stopwatch. If reset is kTRUE reset the stopwatch before // starting it (including the stopwatch counter). // Use kFALSE to continue timing after a Stop() without // resetting the stopwatch. if (reset) { fState = kUndefined; fTotalCpuTime = 0; fTotalRealTime = 0; fCounter = 0; } if (fState != kRunning) { fStartRealTime = GetRealTime(); fStartCpuTime = GetCPUTime(); } fState = kRunning; fCounter++; } //______________________________________________________________________________ inline void Stopwatch::Stop() { // Stop the stopwatch. fStopRealTime = GetRealTime(); fStopCpuTime = GetCPUTime(); if (fState == kRunning) { fTotalCpuTime += fStopCpuTime - fStartCpuTime; fTotalRealTime += fStopRealTime - fStartRealTime; } fState = kStopped; } //______________________________________________________________________________ inline void Stopwatch::Continue() { // Resume a stopped stopwatch. The stopwatch continues counting from the last // Start() onwards (this is like the laptimer function). if (fState == kUndefined){ //cout<< "stopwatch not started"<