精确计算代码执行时间
生活随笔
收集整理的這篇文章主要介紹了
精确计算代码执行时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在一些測試工作時我們需要獲得高精度的代碼執行時間以比較其效率。最近遇到一個模塊其執行時間非常短,但是調用頻率非常高。精確計算其運算時間對于提高程序整體效率來說非常重要。
????? 在我剛剛接觸.Net時,也曾經想要測試一下自己寫的程序的運行時間,當時我使用的是將兩個DateTime.Now相減的笨方法,呵呵。后來知道使用Environment.TickCount,對于一般的測試來說就足夠了。但是它對于高精度測試就沒什么辦法,經常是返回個0了事。對于高精度測試我們應當使用QueryPerformanceFrequency函數和QueryPerformanceCounter函數。通過它們可以獲得比Environment.TickCount更高的精確度。實際上Environment.TickCount就是在調用QueryPerformanceFrequency函數和QueryPerformanceCounter函數。
????? 下面是我使用的代碼: using?System;
class?Class1
{
????[System.Runtime.InteropServices.DllImport?("Kernel32.dll")]
????static?extern?bool?QueryPerformanceCounter(ref?long?count);
????[System.Runtime.InteropServices.DllImport?("Kernel32.dll")]
????static?extern?bool?QueryPerformanceFrequency(ref?long?count);
????[STAThread]
????static?void?Main(string[]?args)
????{
????????long?count?=?0;
????????long?count1?=?0;
????????long?freq?=?0;
????????double?result?=?0;
????????
????????QueryPerformanceFrequency(ref?freq);
????????QueryPerformanceCounter(ref?count);
????????????????//需要測試的模塊
????????
????????QueryPerformanceCounter(ref?count1);
????????count?=?count1-count;
????????result?=?(double)(count)/(double)freq;
????????Console.WriteLine("耗時:?{0}?秒",?result);
????????Console.ReadLine();
????}
}
class?Class1
{
????[System.Runtime.InteropServices.DllImport?("Kernel32.dll")]
????static?extern?bool?QueryPerformanceCounter(ref?long?count);
????[System.Runtime.InteropServices.DllImport?("Kernel32.dll")]
????static?extern?bool?QueryPerformanceFrequency(ref?long?count);
????[STAThread]
????static?void?Main(string[]?args)
????{
????????long?count?=?0;
????????long?count1?=?0;
????????long?freq?=?0;
????????double?result?=?0;
????????
????????QueryPerformanceFrequency(ref?freq);
????????QueryPerformanceCounter(ref?count);
????????//開始的時候沒有這層循環,所得數據浮動很大,添加這層循環來使得結果更加平均
????????for?(int?i=0;?i<500;?i++)?
????????{
????????????????//需要測試的模塊
????????}
????????
????????QueryPerformanceCounter(ref?count1);
????????count?=?count1-count;
????????result?=?(double)(count)/(double)freq;
????????Console.WriteLine("耗時:?{0}?秒",?result);
????????Console.ReadLine();
????}
} ?
????? 在我剛剛接觸.Net時,也曾經想要測試一下自己寫的程序的運行時間,當時我使用的是將兩個DateTime.Now相減的笨方法,呵呵。后來知道使用Environment.TickCount,對于一般的測試來說就足夠了。但是它對于高精度測試就沒什么辦法,經常是返回個0了事。對于高精度測試我們應當使用QueryPerformanceFrequency函數和QueryPerformanceCounter函數。通過它們可以獲得比Environment.TickCount更高的精確度。實際上Environment.TickCount就是在調用QueryPerformanceFrequency函數和QueryPerformanceCounter函數。
????? 下面是我使用的代碼: using?System;
class?Class1
{
????[System.Runtime.InteropServices.DllImport?("Kernel32.dll")]
????static?extern?bool?QueryPerformanceCounter(ref?long?count);
????[System.Runtime.InteropServices.DllImport?("Kernel32.dll")]
????static?extern?bool?QueryPerformanceFrequency(ref?long?count);
????[STAThread]
????static?void?Main(string[]?args)
????{
????????long?count?=?0;
????????long?count1?=?0;
????????long?freq?=?0;
????????double?result?=?0;
????????
????????QueryPerformanceFrequency(ref?freq);
????????QueryPerformanceCounter(ref?count);
????????????????//需要測試的模塊
????????
????????QueryPerformanceCounter(ref?count1);
????????count?=?count1-count;
????????result?=?(double)(count)/(double)freq;
????????Console.WriteLine("耗時:?{0}?秒",?result);
????????Console.ReadLine();
????}
}
????? 這樣能夠得到非常精確的結果。但是模塊每次運行的時間總會有些誤差,而當計算非常精確的時候,這些運行時間的誤差也顯得比較明顯了。為此我對其進行循環多次測試使其誤差平均化,通過多次測試的結果來進行執行效率的分析。
using?System;class?Class1
{
????[System.Runtime.InteropServices.DllImport?("Kernel32.dll")]
????static?extern?bool?QueryPerformanceCounter(ref?long?count);
????[System.Runtime.InteropServices.DllImport?("Kernel32.dll")]
????static?extern?bool?QueryPerformanceFrequency(ref?long?count);
????[STAThread]
????static?void?Main(string[]?args)
????{
????????long?count?=?0;
????????long?count1?=?0;
????????long?freq?=?0;
????????double?result?=?0;
????????
????????QueryPerformanceFrequency(ref?freq);
????????QueryPerformanceCounter(ref?count);
????????//開始的時候沒有這層循環,所得數據浮動很大,添加這層循環來使得結果更加平均
????????for?(int?i=0;?i<500;?i++)?
????????{
????????????????//需要測試的模塊
????????}
????????
????????QueryPerformanceCounter(ref?count1);
????????count?=?count1-count;
????????result?=?(double)(count)/(double)freq;
????????Console.WriteLine("耗時:?{0}?秒",?result);
????????Console.ReadLine();
????}
} ?
轉載于:https://www.cnblogs.com/AloneSword/archive/2007/06/01/2237552.html
總結
以上是生活随笔為你收集整理的精确计算代码执行时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: max与top的效率
- 下一篇: 理解 ActivityExecution