我是 C++ 的新手,我有一个用 C++ 为 Linux 编写的程序。我正在尝试将其转换为 Windows。我的代码是:
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));
}
};
对于 tms_utime
,我在 Visual C++ 中找到术语 QueryPerformanceCounter
,但我无法应用它。
对于 sysconf(_SC_CLK_TCK)
我使用 CLOCKS_PER_SEC
但我不知道这有多正确? Windows 的等效代码是什么?
最佳答案
这是一个直接替换,返回用户时间,而不是耗时:
#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;
}
};
关于c++ - C++ 代码从 Linux 到 Windows 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7830496/