如何从 C# 中的 QueryPerformanceCounter 获取滴答声? [英] How to get ticks from QueryPerformanceCounter in C#?
问题描述
我需要替换 Stopwatch 以避免对其属性使用 getter.我将使用 QueryPerformanceCounter 来实现它.我只需要滴答声 没有别的.
I need to replace Stopwatch to avoid using getters for its properties. I am going to implement it using QueryPerformanceCounter. I only need ticks nothing else.
任何人都可以提供代码片段以获得正确的滴答声(毫秒的 1/10000)或任何其他小但稳定的值.
Can anyone provide a code snipet please, to get correct ticks (1/10000 of ms) or any other small but stable value.
请注意,我的服务器将时钟粒度设置为 0.5 毫秒(不确定它是否会影响 QueryPerformanceCounter),但仅供您了解.
Please note my server sets clock granularity down to 0.5 ms (not sure if it affects QueryPerformanceCounter) but just for you to know.
另请注意 - 我不需要计时器.我只需要测量代码部分之间的时间间隔.
Please also note - I do not need a timer. I only need to measure time intervals between parts of code.
为避免混淆,我真的很想知道 QueryPerformanceCounter(out long lpPerformanceCount) 中的 lpPerformanceCount 是什么;
to avoid confusion I really want to know what is lpPerformanceCount in QueryPerformanceCounter(out long lpPerformanceCount);
推荐答案
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
取自 http://www.codeproject.com/文章/2635/High-Performance-Timer-in-C
旧但它应该仍然有效
StopWatch
的内部实际上使用 QueryPerformanceCounter
,因此使用托管代码应该提供相同的结果,具有更好的兼容性.
The internals of StopWatch
actually use QueryPerformanceCounter
, so using the managed code should provide identical results, with better compatibility.
这篇关于如何从 C# 中的 QueryPerformanceCounter 获取滴答声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!