/** \file Timer.h Header file for Timer class. \author George J. Grevera, Ph.D., ggrevera@sju.edu Copyright (C) 2002, George J. Grevera This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or from http://www.gnu.org/licenses/gpl.txt. This General Public License does not permit incorporating this code into proprietary programs. (So a hypothetical company such as GH (Generally Hectic) should NOT incorporate this code into their proprietary programs.) */ #ifndef Timer_h #define Timer_h //---------------------------------------------------------------------- /// Timer class for reporting elapsed time and CPU time. class Timer { private: double mStart; FILETIME mKernelTime, mUserTime; public: // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - inline Timer ( ) { reset(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// reset timer to 0. inline void reset ( void ) { mStart = GetTickCount(); FILETIME creationTime, exitTime; GetProcessTimes( GetCurrentProcess(), &creationTime, &exitTime, &mKernelTime, &mUserTime ); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// return elapsed time in seconds. inline double getElapsedTime ( void ) { long tc_end = GetTickCount(); return (tc_end-mStart) / 1E3; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// return actual cpu time used. inline double getCPUTime ( void ) { FILETIME creationTime, exitTime, kernelTime, userTime; GetProcessTimes( GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime ); //as recommended by bill: // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/filetime_str.asp ULARGE_INTEGER u2; memcpy( &u2, &userTime, sizeof(userTime) ); __int64* i2 = (__int64*)&u2; ULARGE_INTEGER u1; memcpy( &u1, &mUserTime, sizeof(mUserTime) ); __int64* i1 = (__int64*)&u1; __int64 i3 = *i2 - *i1; //from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/filetime_str.asp // The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC). const double userCPUTime = i3 * 100 / 1E9; //determine kernel cpu time memcpy( &u2, &kernelTime, sizeof(kernelTime) ); i2 = (__int64*)&u2; memcpy( &u1, &mKernelTime, sizeof(mKernelTime) ); i1 = (__int64*)&u1; i3 = *i2 - *i1; const double kernelCPUTime = i3 * 100 / 1E9; return userCPUTime + kernelCPUTime; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }; #endif //----------------------------------------------------------------------