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

发布于 2024-12-11 04:59:23 字数 581 浏览 0 评论 0原文

我是 C++ 新手,我有一个为 Linux 编写的 C++ 程序。我正在尝试将其转换为 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 的等效代码是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甩你一脸翔 2024-12-18 04:59:23

这是一个直接替换,它返回用户时间,而不是经过的时间:

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

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;
    }
};
凉薄对峙 2024-12-18 04:59:23

这是我写的一个类,我总是使用

#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

示例用法:

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

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 ) ;
}
可爱暴击 2024-12-18 04:59:23

这是(未经测试,但逻辑上正确的)替换下降。 usertime 函数以第二分辨率返回(作为 double),因此您需要除以所需的分辨率。

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

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;
   }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文