c++ - C++ 代码从 Linux 到 Windows 的转换

标签 c++ visual-c++

我是 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/

相关文章:

c++ - 向线程发出信号以启动特定功能

c++ - 友元函数声明中的内联说明符

c++ - 带有 g++ 5.4.0 的 asan 无法在 travis CI 上运行

c++ - 如何处理 WM_NCCALCSIZE 并制作类似 chrome 的界面?

c++ - 在 C++ 的二维数组中通过引用传递有问题

c++ - 我如何确定哪个依赖项会导致 C++ 编译单元被重建?

opencv - 在openCV中逐像素复制

c++ - 如何保存自定义 DockWidget

C++ std::initializer_list 用法

visual-c++ - Ogre3d错误: cannot open file OgreMain_d.lib