精确计算代码执行时间


      在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。最近遇到一个模块其执行时间非常短,但是调用频率非常高。精确计算其运算时间对于提高程序整体效率来说非常重要。

      在我刚刚接触.Net时,也曾经想要测试一下自己写的程序的运行时间,当时我使用的是将两个DateTime.Now相减的笨方法,呵呵。后来知道使用Environment.TickCount,对于一般的测试来说就足够了。但是它对于高精度测试就没什么办法,经常是返回个0了事。对于高精度测试我们应当使用QueryPerformanceFrequency函数和QueryPerformanceCounter函数。通过它们可以获得比Environment.TickCount更高的精确度。实际上Environment.TickCount就是在调用QueryPerformanceFrequency函数和QueryPerformanceCounter函数。

      下面是我使用的代码:

None.gif using  System;
None.gif
None.gif
class  Class1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [System.Runtime.InteropServices.DllImport (
"Kernel32.dll")]
InBlock.gif    
static extern bool QueryPerformanceCounter(ref long count);
InBlock.gif
InBlock.gif    [System.Runtime.InteropServices.DllImport (
"Kernel32.dll")]
InBlock.gif    
static extern bool QueryPerformanceFrequency(ref long count);
InBlock.gif
InBlock.gif    [STAThread]
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
long count = 0;
InBlock.gif        
long count1 = 0;
InBlock.gif        
long freq = 0;
InBlock.gif        
double result = 0;
InBlock.gif        
InBlock.gif        QueryPerformanceFrequency(
ref freq);
InBlock.gif        QueryPerformanceCounter(
ref count);
InBlock.gif
InBlock.gif                
//需要测试的模块
InBlock.gif
        
InBlock.gif        QueryPerformanceCounter(
ref count1);
InBlock.gif        count 
= count1-count;
InBlock.gif        result 
= (double)(count)/(double)freq;
InBlock.gif
InBlock.gif        Console.WriteLine(
"耗时: {0} 秒", result);
InBlock.gif        Console.ReadLine();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

      这样能够得到非常精确的结果。但是模块每次运行的时间总会有些误差,而当计算非常精确的时候,这些运行时间的误差也显得比较明显了。为此我对其进行循环多次测试使其误差平均化,通过多次测试的结果来进行执行效率的分析。

None.gif using  System;
None.gif
None.gif
class  Class1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [System.Runtime.InteropServices.DllImport (
"Kernel32.dll")]
InBlock.gif    
static extern bool QueryPerformanceCounter(ref long count);
InBlock.gif
InBlock.gif    [System.Runtime.InteropServices.DllImport (
"Kernel32.dll")]
InBlock.gif    
static extern bool QueryPerformanceFrequency(ref long count);
InBlock.gif
InBlock.gif    [STAThread]
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
long count = 0;
InBlock.gif        
long count1 = 0;
InBlock.gif        
long freq = 0;
InBlock.gif        
double result = 0;
InBlock.gif        
InBlock.gif        QueryPerformanceFrequency(
ref freq);
InBlock.gif        QueryPerformanceCounter(
ref count);
InBlock.gif
InBlock.gif        
//开始的时候没有这层循环,所得数据浮动很大,添加这层循环来使得结果更加平均
InBlock.gif
        for (int i=0; i<500; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                
//需要测试的模块
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
InBlock.gif        QueryPerformanceCounter(
ref count1);
InBlock.gif
InBlock.gif        count 
= count1-count;
InBlock.gif        result 
= (double)(count)/(double)freq;
InBlock.gif
InBlock.gif        Console.WriteLine(
"耗时: {0} 秒", result);
InBlock.gif        Console.ReadLine();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}




 

转载于:https://www.cnblogs.com/aiyagaze/archive/2006/09/23/512507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值