• 【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.  

      

      

  • 相关阅读:
    悼念丹尼斯·里奇,那个给乔布斯提供肩膀的巨人(转载)
    c# 做成Windows服务
    Visual Studio 2010 新建完项目编译就出错
    C#调用Win32 的API函数User32.dll
    c# Remoting小例子
    backgroundworker使用 实现进度条ProgressBar
    winform最小化后隐藏到右下角,单击或双击后恢复
    关于Thread的实例
    c# 捕获的异常写到日志里
    C# delegate and event 规范写法
  • 原文地址:https://www.cnblogs.com/Atanisi/p/8028215.html
Copyright © 2020-2023  润新知