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