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

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++】查看代码运行时间的更多相关文章

  1. 用SWD调试接口测量代码运行时间 ( SWO )

    用SWD调试接口测量代码运行时间 关于时间测量的种种问题 在嵌入式中,我们经常需要测量某段代码的执行时间或测量事件触发的时间,常规的思路是: 1:在测量起始点,反转电平2:在测量结束点,再次反转电平 ...

  2. C#如何测试代码运行时间

    1.System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 // 需要测试 ...

  3. svn查看代码作者的命令

    svn blame **.java | grep ** svn查看代码作者的命令

  4. 使用console进行 性能测试 和 计算代码运行时间(转载)

    本文转载自: 使用console进行 性能测试 和 计算代码运行时间

  5. C# 测试代码运行时间

    一.新建一个控制台程序项目Test.exe using System; using System.Collections.Generic; using System.Linq; using Syste ...

  6. 查看语句运行时间异常的原因(SQLServer)

    转载:http://www.cnblogs.com/fygh/archive/2012/01/17/2324926.html 查看语句运行时间异常的原因(SQLServer)   经常有开发同事反映如 ...

  7. clock_gettime测代码运行时间

    //函数原型: // long clock_gettime (clockid_t which_clock, struct timespec *tp); //参数列表: // CLOCK_REALTIM ...

  8. C# 精准获取代码运行时间

    纯粹转载,转载请注明参考链接及作者! 参考链接:http://www.cnblogs.com/ret00100/archive/2010/08/06/1793680.html,作者:博客园 大佬辉   ...

  9. Objective-C 计算代码运行时间

    今天看到一篇关于iOS应用性能优化的文章,其中提到计算代码的运行时间,觉得非常有用,值得收藏.不过在模拟器和真机上是有差异的,以此方法观察程序运行状态,提高效率. 第一种:(最简单的NSDate) N ...

随机推荐

  1. 九度OJ 1184:二叉树遍历 (二叉树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3515 解决:1400 题目描述: 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储). 例如如下的 ...

  2. Spring 拦截器——HandlerInterceptor

    采用Spring拦截器的方式进行业务处理.HandlerInterceptor拦截器常见的用途有: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2 ...

  3. Struts2之ModelDriven的使用

    http://www.cnblogs.com/luoyanli/archive/2012/11/20/2778361.html 我们可以根据Action属性的不同将它分为两类:Field-Driven ...

  4. mysql复制表结构和内容

    许多时候我们想复制一张表部分或者全部内容,或者只复制一张表的框架,这种需求应该说我们很常见. 其实想要实现这种需求十分简单. 表一: mysql> select * from student_i ...

  5. js阻止a链接

    <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 3.08课·········switch case及if else嵌套(日期格式)

    switch case switch (n) { : break; : break; . . . case n: break; } 1.switch case必须与break一同使用,每一个case后 ...

  7. Android Theme主题

    •android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式 •android:theme="@and ...

  8. python3.7.1 内置函数

    python3.7.1 内置函数列表 内置函数 abs() delattr() hash() memoryview() set() all() dict() help() min() setattr( ...

  9. 第十篇、模块一、sys\os\hashlib模块的应用

    一.模块分为三种 1)自定义模块 2)第三方模块 3)内置模块 如何导入模块? 下面两种: 1)import 模块名字  as 别名(重新给模块命名) 2)from  模块名字  import 功能( ...

  10. SS中的三种样式来源:创作人员、读者和用户代理

    CSS中的样式一共有三种来源:创作人员.读者和用户代理,来源的不同会影响到样式的层叠方式,很多第一次学习CSS的朋友,对这三种来源可能会存在一些困惑,下面我写一下自己的理解,若有错误的地方还请指正. ...