博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

修身,齐家,治国,平天下

liuxwin
liuxwin.cublog.cn


检测代码运行效率 GetTickCount()的使用
   调试程序的时很多时候都想知道自己写的程序运行的效率,也就是说因该得到这段程序运行的时间,我下面介绍这个函数就可以完成这个功能,她就是:GetTickCount()
   下面是MSDN上的解释:
   GetTickCount
     The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.
DWORD GetTickCount(VOID);

Parameters

This function has no parameters.

Return Values

The return value is the number of milliseconds that have elapsed since the system was started.

Remarks

The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.

If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.

Windows NT/2000/XP: To obtain the time elapsed since the computer was started, retrieve the System Up Time counter in the performance data in the registry key HKEY_PERFORMANCE_DATA. The value returned is an 8-byte value. For more information, see Performance Monitoring.

Example Code

The following example demonstrates how to handle timer wrap around.

DWORD dwStart = GetTickCount();

// Stop if this has taken too long
if( GetTickCount() - dwStart >= TIMELIMIT )
    Cancel();
这个函数是取得程序运行到现在的时间,这样在检测代码的前后各运行一次这个函数,两次的差就是程序运行的大概的时间了。例如:
DWORD dwStart;
DWORD dwEnd;
dwStart = GetTickCount();
//要检测的代码
dwEnd = GetTickCount();
CString s ;
s.Format( "%d", dwEnd- dwStart ) ;
AfxMessageBox( s ) ;
这个函数也可以用作定时,例如:

#i nclude <windows.h>

#i nclude <stdio.h>

void main()

{

     DWORD dwLast;

     DWORD dwCurrent;

     DWORD dwInterval = 1000;

 

dwLast = GetTickCount();

     int i = 0;

     while(true)

     {

         dwCurrent = GetTickCount();

         if( dwCurrent - dwLast < dwInterval )

              continue;

         //your code to be executed when interval is elapsed

         printf("dwLast,dwCurrent,diff:%d,%d,%d\n",dwLast,dwCurrent,dwCurrent-dwLast);

         //your code to determine when to break

         if( i > 10 ) break;

         i++;

         dwLast = dwCurrent;

     }

   getchar();  

   return

发表于: 2007-03-21 ,修改于: 2007-03-21 14:36,已浏览2475次,有评论2条 推荐 投诉


网友评论
内容:
不怎么明白了,不过觉得你的那个NOD可以,接着发呀,省得我在去找呀
本站网友评论于:2007-03-23 11:41:59 (124.114.153.★)
内容:
发现了个更简单的方法,打印调试信息法。在程序开始运行时打印一条信息,信息的内容就是当时的时间,检测的代码结束后再打印一条。用DebugView来接收调试信息,根据两条信息就知道被测代码的效率了
liuxwin 评论于:2007-12-04 10:39:35 (61.185.204.★)

发表评论