C# 高性能计时器与秒表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1485839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
High-Performance Timer vs StopWatch
提问by Kane
Does anyone know if the HiPerfTimeror the StopWatchclass is better for benchmarking, and why?
有谁知道HiPerfTimer或StopWatch类是否更适合进行基准测试,为什么?
采纳答案by Shay Erlichmen
Stopwatchis based on High resolution timer (where available), you can check that with IsHighResolution
秒表基于高分辨率计时器(如果可用),您可以使用IsHighResolution进行检查
回答by RichardOD
StopWatch- it also works on systems that don't support a high resolution performance counter and you don't need any external libraries to use it.
秒表 - 它也适用于不支持高分辨率性能计数器的系统,并且您不需要任何外部库来使用它。
The other one throws an Win32Exception if there is no support for a high resolution counter.
如果不支持高分辨率计数器,另一个将引发 Win32Exception。
回答by ParmesanCodice
They are the same when it comes to high resolution timing.
在高分辨率时序方面,它们是相同的。
Both use this:
两者都使用这个:
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long PerformanceCount);
and this:
和这个:
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long Frequency);
to do the underlying timing. (You can verify this with Reflector.NET). I'd use StopWatch because it's part of the framework already (no need to link another dll) and it had better features than HiPerfTimer.
做底层时序。(您可以使用 Reflector.NET 验证这一点)。我会使用 StopWatch,因为它已经是框架的一部分(不需要链接另一个 dll)并且它的功能比 HiPerfTimer 更好。