测试程序片段执行时间的代码段

在编程过程中经常遇到测试一段程序执行时间的问题,下面给出两种测试代码段执行时间的方法以供参考。

第一种方法:低精度,GetTickCount()函数的精度只有10ms,所以如果程序执行时间小于10ms,不能使用该函数
DWORD Milliseconds_Parser_Start;
DWORD Milliseconds_Parser_End;
Milliseconds_Parser_Start = ::GetTickCount();

// 待测试代码段

Milliseconds_Parser_End = ::GetTickCount();
TCHAR debugStr[256];
_stprintf(szDebug, "item: %d milin sec /n",Milliseconds_Parser_End - Milliseconds_Parser_Start);
OutputDebugString(debugStr);

 

第二种方法:高精度
LARGE_INTEGER t_freqency;
QueryPerformanceFrequency(&t_freqency);
float f_countDiff2Milisecond = float(1000.0/t_freqency.QuadPart);

LARGE_INTEGER start;
LARGE_INTEGER end;
QueryPerformanceCounter(&start);


// 待测试代码段

QueryPerformanceCounter(&end);
TCHAR debugStr[256];
_stprintf(debugStr, _T("item: %f milin sec /n"),(end.QuadPart - start.QuadPart) * f_countDiff2Milisecond );
OutputDebugString(debugStr);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值