Is it possible to read the internal CPU tick counter from C#?

前端 未结 3 1550
梦毁少年i
梦毁少年i 2021-01-14 09:50

I have a multithreaded C# program where I need to log how many ticks each thread spends in a specific spin wait lock.

I know there are methods for doing that from C

3条回答
  •  孤街浪徒
    2021-01-14 10:29

    Look into StopWatch class which is based upon *QueryPerformance** APIs is precisely for this purpose.

    MSDN:

    The Stopwatch class assists the manipulation of timing-related performance counters within managed code. Specifically, the Frequency field and GetTimestamp method can be used in place of the unmanaged Win32 APIs QueryPerformanceFrequency and QueryPerformanceCounter.

    Also results collected by Kristof Verbiest is worth a glipse.

提交回复
热议问题