发博文
平凡者

kaywin.blog.chinaunix.net

   
个人资料
  • 博客访问:308932
  • 博文数量:141
  • 博客积分:7078
  • 博客等级:少将
  • 注册时间:2006-03-16 08:53:10
订阅我的博客
  • 订阅
  • 订阅到鲜果
  • 订阅到抓虾
  • 订阅到Google
字体大小: 博文
分类: C/C++/VC

   调试程序的时很多时候都想知道自己写的程序运行的效率,也就是说因该得到这段程序运行的时间,我下面介绍这个函数就可以完成这个功能,她就是: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

[发评论] 评论 重要提示:警惕虚假中奖信息!
  • chinaunix网友 2011-05-18 15:40
    毛,GetTickCount();精度只有10ms,等他检测到,程序都不知跑了多少次了,头脑简单。
  • liuxwin 2007-12-04 10:39
    发现了个更简单的方法,打印调试信息法。在程序开始运行时打印一条信息,信息的内容就是当时的时间,检测的代码结束后再打印一条。用DebugView来接收调试信息,根据两条信息就知道被测代码的效率了
  • chinaunix网友 2007-03-23 11:41
    不怎么明白了,不过觉得你的那个NOD可以,接着发呀,省得我在去找呀
亲,您还没有登录,请[登录][注册]后再进行评论