Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of C++ code from Linux to Windows

Tags:

c++

visual-c++

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?

like image 205
Mohammad Arifuzzaman Avatar asked Jan 21 '26 11:01

Mohammad Arifuzzaman


1 Answers

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;
    }
};
like image 181
clstrfsck Avatar answered Jan 23 '26 01:01

clstrfsck