Chinaunix首页 | 论坛 | 博客
  • 博客访问: 751979
  • 博文数量: 128
  • 博客积分: 7079
  • 博客等级: 少将
  • 技术积分: 1326
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-16 08:53
文章分类

全部博文(128)

文章存档

2011年(3)

2010年(12)

2009年(9)

2008年(23)

2007年(61)

2006年(20)

我的朋友

分类: C/C++

2007-03-21 14:36:12

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

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 .

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

#i nclude

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

阅读(4985) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-05-18 15:40:53

毛,GetTickCount();精度只有10ms,等他检测到,程序都不知跑了多少次了,头脑简单。