码迷,mamicode.com
首页 > 编程语言 > 详细

【C&C++】查看代码运行时间

时间:2017-12-12 17:31:03      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:使用   pos   lis   nts   cti   发送   device   blank   microsoft   

  查看代码运行时间有助于更好地优化项目代码

1. Windows平台

  windows平台下有两种方式,精度有所不同,都需要包含<windows.h>头文件

1) DWORD GetTickCount(void); 返回毫秒数

  官方文档:(3/28/2014) msdn

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 zero on boot and then counts up from there.

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

  该函数有几点需要注意:

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

  b) For debug configurations, 180 seconds is subtracted from the the number of milliseconds since the device booted. 

  c) On the STMicro platform, the time returned by GetTickCount includes a ~0.014% time drift, which is by design. This translates to a time lag of approximately 1 second    every 2 hours.

  d) 实际使用中,发现该函数相当不精确,对于运行时间只有10毫秒以内的代码段,经常显示0,百度百科中有讲到这个函数并非实时发送,而是由系统每18ms发送一次,因此其最小精度为18ms。当需要有小于18ms的精度计算时,应使用StopWatch或其它方法进行。连续触发200次,实测下来,最小间隔在15ms。实际状况应该是系统每秒触发64次,间隔在15、16ms之间波动。

 

2)  BOOL WINAPI QueryPerformanceCounter( _Out_ LARGE_INTEGER *lpPerformanceCount); 返回微秒数

  官方文档:msdn

  Retrieves the current value of the performance counter, which is a high resolution (<1us) time stamp that can be used for time-interval measurements.  

  

  

【C&C++】查看代码运行时间

标签:使用   pos   lis   nts   cti   发送   device   blank   microsoft   

原文地址:http://www.cnblogs.com/Atanisi/p/8028215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!