是否可以从C#读取内部CPU滴答计数器? [英] Is it possible to read the internal CPU tick counter from C#?

查看:212
本文介绍了是否可以从C#读取内部CPU滴答计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多线程C#程序,需要记录每个线程在特定的自旋等待锁中花费的滴答声。



我知道有一些方法可以从C或汇编程序中执行此操作,但是是否可以通过某种方式直接从C#访问同一计数器,即无需通过Stopwatch类(我假设在其中调用Start / Stop会产生一些开销,并且我不确定它的精确度)?

解决方案

在使用System.Diagnostics.Stopwatch淘汰之前:您的测试系统是否支持高分辨率秒表? :请参阅 StopWatch:结帐:上的 MSDN。频率和'IsHighResolution



出色的SO线程,可以使StopWatch中的最佳获得: SO线程不要错过Eric Lippert的评论



精度与准确性在.net时间测量中: SO线程:.NET时间测量中的精度与准确性可能会有所帮助。 p>

在C#中Win32 API的API调用的成本:QueryPerformanceFrequency和QueryPerformanceCounter:与StopWatch相比?



一个API示例来自C#,位于Codeproject上: 2006年代码,作者在评论中说他没有考虑线程s afety



SO上的其他线程已经解决了一些问题,例如补偿了托管代码的一次JIT命中(空运行预热技术),强制垃圾回收和终结器等。


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 or assembler, but is it possible from to access the same counter directly from C# in some way, that is, without going through the Stopwatch class (I assume calling Start/Stop on that has some overhead, and I am not sure how precise it is)?

解决方案

Before you "rule out" using System.Diagnostics.Stopwatch : does your test system support high-resolution stopwatch ? : see MSDN on 'StopWatch : check-out : 'Frequency and 'IsHighResolution.

Excellent SO thread on getting the "best" out of StopWatch : SO thread don't miss comments by Eric Lippert

Precision vs. accuracy in .net time measurement : SO thread : Precision vs. accuracy in .NET time measurement may be helpful.

"Cost" of API calls in C# to Win32 APIs : QueryPerformanceFrequency and QueryPerformanceCounter : compared to StopWatch ?

One API example from C# here on Codeproject : 2006 code, author in a comment says he did not consider thread safety

Other threads here on SO have dealt with issues like compensating for the one-time JIT hit of managed code ("dry run" warm-up technique), forcing Garbage Collection, and Finalizers, etc. Easy to find.

这篇关于是否可以从C#读取内部CPU滴答计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆