Conversion of C++ code from Linux to Windows

前端 未结 3 1813

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


        
相关标签:
3条回答
  • 2021-01-21 00:46

    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;
        }
    };
    
    0 讨论(0)
  • 2021-01-21 00:54

    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 ) ;
    }
    
    0 讨论(0)
  • 2021-01-21 00:59

    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;
       }
    };
    
    0 讨论(0)
提交回复
热议问题