zoukankan      html  css  js  c++  java
  • 【C&C++】查看代码运行时间

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

    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.  

      

      

  • 相关阅读:
    涂抹mysql笔记-安装mysql
    使用Oracle DBLink进行数据库之间对象的访问操作
    mysqldump备份与恢复笔记
    linux6下源码安装mysql5.6
    虚拟机克隆之后,网卡名称从eth0变成eth1之后的解决办法
    linux添加zabbix service并开机自动启动
    理解Linux系统负荷load average
    oracle体系结构理解
    C#中Abstract和Virtual[转载]
    WPF后台写ControlTemplate总结
  • 原文地址:https://www.cnblogs.com/Atanisi/p/8028215.html
Copyright © 2011-2022 走看看