I am new to C++ , I have a program in C++ written for Linux. I'm trying to convert it to Windows. The code I have is:
struct Timer
{
struct tms t[2];
void STARTTIME (void)
{
times(t);
}
void STOPTIME(void)
{
times(t+1);
}
double USERTIME(void)
{
return ((double)((t+1)->tms_utime - t->tms_utime))/((double)sysconf(_SC_CLK_TCK));
}
};
For tms_utime I find term QueryPerformanceCounter in Visual C++, but I cannot apply this.
For sysconf(_SC_CLK_TCK) I use CLOCKS_PER_SEC but I do not know how correct this is? What is the equivalent code for Windows?
Here is a drop-in replacement that returns the user time, rather than the elapsed time:
#include <windows.h>
struct Timer
{
ULONGLONG t[2];
void STARTTIME (void)
{
t[0] = getCurrentUserTime();
}
void STOPTIME(void)
{
t[1] = getCurrentUserTime();
}
double USERTIME(void)
{
return (t[1] - t[0]) / 1e7;
}
private:
// Return current user time in units of 100ns.
// See http://msdn.microsoft.com/en-us/library/ms683223
// for documentation on GetProcessTimes()
ULONGLONG getCurrentUserTime()
{
FILETIME ct, et, kt, ut;
GetProcessTimes(GetCurrentProcess(), &ct, &et, &kt, &ut);
ULARGE_INTEGER t;
t.HighPart = ut.dwHighDateTime;
t.LowPart = ut.dwLowDateTime;
return t.QuadPart;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With