如何获得在C#中从QueryPerformanceCounter的蜱? [英] How to get ticks from QueryPerformanceCounter in C#?

查看:179
本文介绍了如何获得在C#中从QueryPerformanceCounter的蜱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更换秒表避免使用干将其性能。我将使用QueryPerformanceCounter的实现它。 我只需要蜱没有别的。

任何人都可以提供一个code snipet请,以获得正确的蜱(1/10000的MS)或任何其他的小而稳定的价值。

请注意我的服务器组时钟粒度到 0.5毫秒(不知道这是否会影响QueryPerformanceCounter的),但只是让你知道。

请也注意到 - 我并不需要一个计时器。我只需要测量code部位之间的时间间隔。

编辑:,以避免混淆,我真的想知道什么是 lpPerformanceCount 在QueryPerformanceCounter的(出长lpPerformanceCount);

解决方案

  [的DllImport(Kernel32.dll中)
私有静态的extern BOOL QueryPerformanceCounter的(出长lpPerformanceCount);

[的DllImport(Kernel32.dll中)]
私人静态外部布尔QueryPerformanceFrequency的(出长lpFrequency);
 

HTTP措施://www.codeproject.com /用品/ 2635 /高性能定时器 - 在-C

旧,但它应该仍然工作

编辑:的内部秒表实际使用 QueryPerformanceCounter的,所以使用管理code应该提供相同结果,具有较好的相容性。

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.

Can anyone provide a code snipet please, to get correct ticks (1/10000 of ms) or any other small but stable value.

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.

EDIT: 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);

Taken from http://www.codeproject.com/Articles/2635/High-Performance-Timer-in-C

Old but it should still work

EDIT: The internals of StopWatch actually use QueryPerformanceCounter, so using the managed code should provide identical results, with better compatibility.

这篇关于如何获得在C#中从QueryPerformanceCounter的蜱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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