从 Linux 到 Windows 的 C++ 代码转换 [英] Conversion of C++ code from Linux to Windows
本文介绍了从 Linux 到 Windows 的 C++ 代码转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 C++ 新手,我有一个为 Linux 编写的 C++ 程序.我正在尝试将其转换为 Windows.我的代码是:
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));
}
};
对于 tms_utime
,我在 Visual C++ 中找到术语 QueryPerformanceCounter
,但我不能应用它.对于 sysconf(_SC_CLK_TCK)
我使用 CLOCKS_PER_SEC
但我不知道这有多正确?Windows 的等效代码是什么?
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;
}
};
这篇关于从 Linux 到 Windows 的 C++ 代码转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文