#ifndef __tcpu_exe__h #define __tcpu_exe__h #include #include #include #include #include #include #include /* time meter of a running EXTERNAL process or its own process - see constructor */ class TimeCpu { private: union{ FILETIME f; __int64 t; }Cr, Ex, KernelStart, UserStart, KernelStop, UserStop; HANDLE hProcess; public: TimeCpu() { // for the current process hProcess=GetCurrentProcess(); //printf("hProcess=GetCurrentProcess() = %p\n", hProcess); } TimeCpu(HANDLE Process) { // for the "specified" process hProcess=Process; //printf("HANDLE Process = %p\n", hProcess); } void StartTimer(){ //start the timer if ( ! GetProcessTimes(hProcess, &Cr.f, &Ex.f, &KernelStart.f, &UserStart.f) ) { printf("***** Error in StartTimer %d *******\n", GetLastError() ); exit (666);}; KernelStop=KernelStart; UserStop=UserStart; } void StopTimer(){//остановка таймера if ( ! GetProcessTimes(hProcess, &Cr.f, &Ex.f, &KernelStop.f, &UserStop.f) ) { printf("***** Error in StopTimer %d *******\n", GetLastError() ); exit (666);}; } __int64 GetTime(){ //time between start and stop in hundreds of ns. return KernelStop.t-KernelStart.t+UserStop.t-UserStart.t; } __int64 GetCurrTime(){ //time between launch and current moment in hundreds of ns (without stopping). if ( ! GetProcessTimes(hProcess, &Cr.f, &Ex.f, &KernelStop.f, &UserStop.f) ) { printf("***** Error in GetCurrTime %d *****\n", GetLastError()); exit (666);}; return KernelStop.t-KernelStart.t+UserStop.t-UserStart.t; } }; __int64 round_msec(__int64); void sys_time(char *); __int64 round_msec(__int64 time) /**************************************************************************/ /* Rounds 100ths of a nanosecond to milliseconds */ /* Input - time in 100ths of a nanosecond (in __int64 double-word format) */ /* Output - time in milliseconds (in __int64 double-word format) */ /* */ /**************************************************************************/ { __int64 msec; int ost_msec; msec=time/10; //!!! ost_msec=(int) (time-msec*10); if (ost_msec >= 5 ) msec=msec+1; return msec; }; void sys_time(const char *str) /*************************************************************************/ { SYSTEMTIME SystemTime; //current astronomical time /* get astronomical time.... */ GetSystemTime(&SystemTime); //The GetSystemTime function retrieves the current //system date and time. //The system time is expressed in Coordinated Universal Time (UTC). printf("%s", str); printf( " Час=%02d, Min=%02d, Sec=%02d, miliSec=%03d\n", SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds ); return; }; #endif