简体   繁体   中英

Conversion of C++ code from Linux to 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));
    }
};

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's a class I wrote that I always use

#ifndef HIGHPERFTIMER_H
#define HIGHPERFTIMER_H

#include <windows.h>
#include <stdio.h>

class StopWatch
{
  LARGE_INTEGER freq, startTime, endTime, thisTime, lastTime ;
  double fFreq ;

public:
  double total_time ; 

  StopWatch()
  {
    QueryPerformanceFrequency( &freq ) ;
    fFreq = (double)freq.QuadPart ;
    total_time = 0 ;

    printf( "     --- The ffreq is %lf\n", fFreq ) ;
  }

  void start()
  {
    QueryPerformanceCounter( &startTime ) ;
    thisTime = lastTime = startTime ;
    total_time = 0.0 ;  // start counter at 0 seconds
  }

  double stop()
  {
    QueryPerformanceCounter( &endTime ) ;
    total_time = ( endTime.QuadPart - startTime.QuadPart ) / fFreq ;
    return total_time ;
  }

  void update()
  {
    lastTime = thisTime ;
    QueryPerformanceCounter( &thisTime ) ;
    total_time += ( thisTime.QuadPart - lastTime.QuadPart ) / fFreq ;
  }
} ;

#endif //HIGHPERFTIMER_H

Example usage:

int main()
{
    StopWatch stopWatch ;
    stopWatch.start() ;
    ///.. code..
    stopWatch.stop() ;
    printf( "Time elapsed: %f sec", stopWatch.total_time ) ;
}

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;
    }
};

This is (an untested, but logically correct) drop in replacement. The usertime function returns in second resolution (as a double ) so you need to divide by the required resolution.

struct Timer
{
   __int64 t[2];

   void Start()
   {
      QueryPerformanceCounter((LARGE_INTEGER*)&t[0]);
   }

   void Stop()
   {
      QueryPerformanceCounter((LARGE_INTEGER*)&t[1]);
   }

   double usertime()
   {
      __int64 freq;
      QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
      return (double(t[1] - t[0])) / freq;
   }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM