[WorldWind学习]18.High-Performance Timer in C#
2013-08-08 22:16
344 查看
In some applications exact time measurement methods are very important.
一些应用程序中精确的时间测量是非常重要的。
The often used Windows API method GetTickCount() retrieves the number of milliseconds that have elapsed since the system was started, but the GetTickCount() function only archieve resolutions of 1ms and on the other side they are very imprecise.
常使用WindowApi方法GetTickCount()提取系统启动后使用的毫秒,但是GetTickCount()方法只检索1ms的精度,另一方面他也是非常不精确的
So, for exact time measurement we should find another method.
因此,为了提取精确的时间,我们需要寻找其他的方法。
High resolution timing is supported in Win32 by the QueryPerformanceCounter() and QueryPerformanceFrequency() API methods.
Win32通过QueryPerformanceCounter()和QueryPerformanceFrequency() API支持高精度的时间
This timer functions has much better resolution than the "standard" millisecond-based timer calls, like the GetTickCount() method.
这个时间函数比标准的毫秒级要精确的多
On the other side there is also a little bit overhead when calling this "unmanaged" API methods from C#, but it's better than using the very imprecise GetTickCount() API function.
也有一些开销用非托管的API方法,但是比使用不精确的GetTickCount() API好的多。
The first call, QueryPerformanceCounter(), queries the actual value of the high-resolution performance counter at any point.
The second function, QueryPerformanceFrequency(), will return the number of counts per second that the high-resolution counter performs.
To retrieve the elapsed time of a code section you have to get the actual value of the high-resolution performance counter immediately before and immediately after the section of code to be timed.
The difference of these values would indicate the counts that elapsed while the code executed.
这些值得变化可以指示代码执行花费的时间。
The elapsed time can be computed then, by dividing this difference by the number of counts per second that the high-resolution counter performs (the frequency of the high-resolution timer).
花费时间就可以计算,通过除以高精度时间的频率
duration = (stop - start) / frequency
For more information about QueryPerformanceCounter and QueryPerformanceFrequency read the documentation on MSDN.
http://www.codeproject.com/Articles/2635/High-Performance-Timer-in-C
WW中的类:
备注:C#可以采用下面方案实现 :
Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间。
Stopwatch 类为托管代码内与计时有关的性能计数器的操作提供帮助。 具体说来, Frequency 字段和 GetTimestamp 方法可以用于替换非托管 Win32 APIQueryPerformanceFrequency 和 QueryPerformanceCounter。
一些应用程序中精确的时间测量是非常重要的。
The often used Windows API method GetTickCount() retrieves the number of milliseconds that have elapsed since the system was started, but the GetTickCount() function only archieve resolutions of 1ms and on the other side they are very imprecise.
常使用WindowApi方法GetTickCount()提取系统启动后使用的毫秒,但是GetTickCount()方法只检索1ms的精度,另一方面他也是非常不精确的
So, for exact time measurement we should find another method.
因此,为了提取精确的时间,我们需要寻找其他的方法。
High resolution timing is supported in Win32 by the QueryPerformanceCounter() and QueryPerformanceFrequency() API methods.
Win32通过QueryPerformanceCounter()和QueryPerformanceFrequency() API支持高精度的时间
This timer functions has much better resolution than the "standard" millisecond-based timer calls, like the GetTickCount() method.
这个时间函数比标准的毫秒级要精确的多
On the other side there is also a little bit overhead when calling this "unmanaged" API methods from C#, but it's better than using the very imprecise GetTickCount() API function.
也有一些开销用非托管的API方法,但是比使用不精确的GetTickCount() API好的多。
The first call, QueryPerformanceCounter(), queries the actual value of the high-resolution performance counter at any point.
The second function, QueryPerformanceFrequency(), will return the number of counts per second that the high-resolution counter performs.
To retrieve the elapsed time of a code section you have to get the actual value of the high-resolution performance counter immediately before and immediately after the section of code to be timed.
The difference of these values would indicate the counts that elapsed while the code executed.
这些值得变化可以指示代码执行花费的时间。
The elapsed time can be computed then, by dividing this difference by the number of counts per second that the high-resolution counter performs (the frequency of the high-resolution timer).
花费时间就可以计算,通过除以高精度时间的频率
duration = (stop - start) / frequency
For more information about QueryPerformanceCounter and QueryPerformanceFrequency read the documentation on MSDN.
http://www.codeproject.com/Articles/2635/High-Performance-Timer-in-C
WW中的类:
using System; using System.Runtime.InteropServices; namespace WorldWind { public sealed class PerformanceTimer { #region Instance Data public static long TicksPerSecond; #endregion #region Creation /// <summary> /// Static class /// </summary> private PerformanceTimer() { } /// <summary> /// Static constructor /// </summary> static PerformanceTimer() { // Read timer frequency long tickFrequency = 0; if (!QueryPerformanceFrequency(ref tickFrequency)) throw new NotSupportedException("The machine doesn't appear to support high resolution timer."); TicksPerSecond = tickFrequency; System.Diagnostics.Debug.WriteLine("tickFrequency = " + tickFrequency); } #endregion #region High Resolution Timer functions [System.Security.SuppressUnmanagedCodeSecurity] [DllImport("kernel32")] private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency); [System.Security.SuppressUnmanagedCodeSecurity] [DllImport("kernel32")] public static extern bool QueryPerformanceCounter(ref long PerformanceCount); #endregion } }
备注:C#可以采用下面方案实现 :
Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间。
Stopwatch 类为托管代码内与计时有关的性能计数器的操作提供帮助。 具体说来, Frequency 字段和 GetTimestamp 方法可以用于替换非托管 Win32 APIQueryPerformanceFrequency 和 QueryPerformanceCounter。
相关文章推荐
- High-Performance Timer in C#
- How to write the fast code/ high performance in C#
- There is an internal error in the React performance measurement code.Did not expect componentDidMount timer to start while render timer is still in progress for another instance
- C#学习笔记18-匿名方法和Lambda表达式
- High Resolution Timer in Java 5
- KING_C#学习之Thread和Timer实现延时用法(附自动切换窗体实例)
- .net垃圾回收学习【NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code】【翻译&&学习】
- C#学习常用方法(3000)---Foreach ,in
- C#学习之time控件和timer_tick事件
- performance optimization in C#
- High performance models in TensorFlow
- C# in Depth Third Edition 学习笔记-- Lambda表达式和表达式树
- C#学习Timer
- C# in Depth Third Edition 学习笔记-- C#2.0: 解决C#1.0的问题 1 泛型
- C# SocketAsyncEventArgs High Performance Socket Code
- C#学习常用类(1003)---Timer类(System.Timers.Timer)
- Optimizing C# Refactoring Performance with Web Projects in VS 2005
- Implementing High Performance Parsers in Java
- C# in Depth Third Edition 学习笔记-- 值类型和引用
- Timer in C#