日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

[WorldWind学习]18.High-Performance Timer in C#

發(fā)布時間:2025/1/21 C# 101 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [WorldWind学习]18.High-Performance Timer in C# 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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()提取系統(tǒng)啟動后使用的毫秒,但是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.

這個時間函數(shù)比標準的毫秒級要精確的多

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.

這些值得變化可以指示代碼執(zhí)行花費的時間。

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中的類:

1 using System; 2 using System.Runtime.InteropServices; 3 4 namespace WorldWind 5 { 6 public sealed class PerformanceTimer 7 { 8 #region Instance Data 9 10 public static long TicksPerSecond; 11 #endregion 12 13 #region Creation 14 15 /// <summary> 16 /// Static class 17 /// </summary> 18 private PerformanceTimer() 19 { 20 } 21 22 /// <summary> 23 /// Static constructor 24 /// </summary> 25 static PerformanceTimer() 26 { 27 // Read timer frequency 28 long tickFrequency = 0; 29 if (!QueryPerformanceFrequency(ref tickFrequency)) 30 throw new NotSupportedException("The machine doesn't appear to support high resolution timer."); 31 TicksPerSecond = tickFrequency; 32 33 System.Diagnostics.Debug.WriteLine("tickFrequency = " + tickFrequency); 34 } 35 #endregion 36 37 #region High Resolution Timer functions 38 39 [System.Security.SuppressUnmanagedCodeSecurity] 40 [DllImport("kernel32")] 41 private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency); 42 43 [System.Security.SuppressUnmanagedCodeSecurity] 44 [DllImport("kernel32")] 45 public static extern bool QueryPerformanceCounter(ref long PerformanceCount); 46 47 #endregion 48 } 49 }

備注:C#可以采用下面方案實現(xiàn)?:

Stopwatch?實例可以測量一個時間間隔的運行時間,也可以測量多個時間間隔的總運行時間。

Stopwatch?類為托管代碼內(nèi)與計時有關(guān)的性能計數(shù)器的操作提供幫助。?具體說來,?Frequency?字段和?GetTimestamp?方法可以用于替換非托管 Win32 APIQueryPerformanceFrequency??QueryPerformanceCounter

總結(jié)

以上是生活随笔為你收集整理的[WorldWind学习]18.High-Performance Timer in C#的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。