美文网首页
Delphi计算程序运行时间

Delphi计算程序运行时间

作者: 大龙10 | 来源:发表于2022-03-03 08:59 被阅读0次
参考资料: 许明吉博客https://www.cnblogs.com/jxsoft/archive/2011/10/17/2215366.html

一、GetTickCount()函数

1、Release版本计时

  • For Release configurations, this function returns the number of milliseconds since the device booted, excluding any time that the system was suspended. GetTickCount starts at 0 on boot and then counts up from there.

  • 在Release版本中,该函数从0开始计时,返回自设备启动后的毫秒数(不含系统暂停时间)。

2、Debug版本计时

  • For Debug configurations, 180 seconds is subtracted from the the number of milliseconds since the device booted. This allows code that uses GetTickCount to be easily tested for correct overflow handling.

  • 在Debug版本中,设备启动后便从计时器中减去180秒。这样方便测试使用该函数的代码的正确溢出处理。

3、Return Values

  • The number of milliseconds indicates success.

  • 返回值:如正确,返回毫秒数。

4、库与头文件

  • Header: Winbase.h.
  • Link Library: Coredll.lib.

二、应用

1、用来计算某个操作所使用的时间:

var
  StartTime, EndTime: cardinal;
begin
  StartTime := GetTickCount;
  sleep(1000);
  EndTime := GetTickCount;
  caption := (IntToStr(EndTime - StartTime) + ' ms');
end;

2、用来定时:

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 ",dwLast,dwCurrent,dwCurrent-dwLast);
         //your code to determine when to break
        if( i > 10 ) break;
        i++;
        dwLast = dwCurrent;
        printf("Time is up!");
        break;
     }
   getchar();   
   return;
} 

三、应用范围

  对于一般的实时控制,使用GetTickCount()函数就可以满足精度要求。
  但要进一步提高计时精度,就要采用QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数。
  这两个函数是VC提供的仅供Windows 9X使用的高精度时间函数,并要求计算机从硬件上支持高精度计时器。

相关文章

网友评论

      本文标题:Delphi计算程序运行时间

      本文链接:https://www.haomeiwen.com/subject/aseyrrtx.html