Windows C++ nanosecond timing?

后端 未结 4 546
星月不相逢
星月不相逢 2021-01-14 12:36

Is there a way in C++ on windows to measure time in nanoseconds?

All i can find are linux solutions.

相关标签:
4条回答
  • 2021-01-14 12:49

    If you can run your own assembly, you could read the CPU's cycle counter and divide a cycle difference it by the CPU's clock rate:

    static inline uint64_t get_cycles()
    {
      uint64_t t;
      __asm__ __volatile__ ("rdtsc" : "=A"(t));
      return t;
    }
    
    0 讨论(0)
  • 2021-01-14 12:49

    Use Windows7 and the Hardware Counter Profiling API http://msdn.microsoft.com/en-us/library/windows/desktop/dd796395(v=vs.85).aspx

    Both rdtsc and QueryPerformanceCounter/QueryPerformanceFrequency are not accurate enough because of the large overhead, interrupts and task switches.

    [EDIT]: Sorry mixed up the link for PerformanceCounter with Hardware Counters. Sorry have used it only once and this was a quick answer.

    0 讨论(0)
  • 2021-01-14 12:52

    Use the QueryPerformanceFrequency function to see what speed the QueryPerformanceCounter runs at. I think it might be in the nanosecond range.

    0 讨论(0)
  • 2021-01-14 13:07

    Look into QueryPerformanceCounter on windows.

    When timing code to identify performance bottlenecks, you want to use the highest resolution timer the system has to offer. This article describes how to use the QueryPerformanceCounter function to time application code

    http://support.microsoft.com/kb/172338

    0 讨论(0)
提交回复
热议问题