本文转自MoreWindows 特此标识感谢

http://blog.csdn.net/morewindows/article/details/6854764

本文对Windows平台下常用的计时函数进行总结,包括精度为秒、毫秒、微秒三种精度的5种方法。分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock()不仅可以用在Windows系统,也可以用于Linux系统。在Windows系统下三种,使用Windows提供的API接口timeGetTime()、GetTickCount()及QueryPerformanceCounter()来完成。文章最后给出了5种计时方法示例代码。

标准C/C++的二个计时函数time()及clock()

time_t time(time_t *timer);

返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。

time_t实际是个long长整型typedef long time_t;

头文件:#include <time.h>

clock_t clock(void);

返回进程启动到调用函数时所经过的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock),以毫秒为单位。

clock_t实际是个long长整型typedef long clock_t;

头文件:#include <time.h>

Windows系统API函数

timeGetTime()、GetTickCount()及QueryPerformanceCounter()

DWORD timeGetTime(VOID);

返回系统时间,以毫秒为单位。系统时间是从系统启动到调用函数时所经过的毫秒数。注意,这个值是32位的,会在0到2^32之间循环,约49.71天。

头文件:#include <Mmsystem.h>

引用库:#pragma comment(lib, "Winmm.lib")

DWORD WINAPI GetTickCount(void);

这个函数和timeGetTime()一样也是返回系统时间,以毫秒为单位。

头文件:直接使用#include <windows.h>就可以了。

高精度计时,以微秒为单位(1毫秒=1000微秒)。

先看二个函数的定义

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);

得到高精度计时器的值(如果存在这样的计时器)。

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

返回硬件支持的高精度计数器的频率(次每秒),返回0表示失败。

再看看LARGE_INTEGER

它其实是一个联合体,可以得到__int64 QuadPart;也可以分别得到低32位DWORD LowPart和高32位的值LONG HighPart。

在使用时,先使用QueryPerformanceFrequency()得到计数器的频率,再计算二次调用QueryPerformanceCounter()所得的计时器值之差,用差去除以频率就得到精确的计时了。

头文件:直接使用#include <windows.h>就可以了。

//Windows系统下time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()来计时 by MoreWindows
#include <stdio.h>
#include <windows.h>
#include <time.h> //time_t time() clock_t clock()
#include <Mmsystem.h> //timeGetTime()
#pragma comment(lib, "Winmm.lib") //timeGetTime() int main()
{
//用time()来计时 秒
time_t timeBegin, timeEnd;
timeBegin = time(NULL);
Sleep();
timeEnd = time(NULL);
printf("%d\n", timeEnd - timeBegin); //用clock()来计时 毫秒
clock_t clockBegin, clockEnd;
clockBegin = clock();
Sleep();
clockEnd = clock();
printf("%d\n", clockEnd - clockBegin); //用timeGetTime()来计时 毫秒
DWORD dwBegin, dwEnd;
dwBegin = timeGetTime();
Sleep();
dwEnd = timeGetTime();
printf("%d\n", dwEnd - dwBegin); //用GetTickCount()来计时 毫秒
DWORD dwGTCBegin, dwGTCEnd;
dwGTCBegin = GetTickCount();
Sleep();
dwGTCEnd = GetTickCount();
printf("%d\n", dwGTCEnd - dwGTCBegin); //用QueryPerformanceCounter()来计时 微秒
LARGE_INTEGER large_interger;
double dff;
__int64 c1, c2;
QueryPerformanceFrequency(&large_interger);
dff = large_interger.QuadPart;
QueryPerformanceCounter(&large_interger);
c1 = large_interger.QuadPart;
Sleep();
QueryPerformanceCounter(&large_interger);
c2 = large_interger.QuadPart;
printf("本机高精度计时器频率%lf\n", dff);
printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64d\n", c1, c2, c2 - c1);
printf("计时%lf毫秒\n", (c2 - c1) * / dff); printf("By MoreWindows\n");
return ;
}

最新文章

  1. 多线程之信号量(By C++)
  2. 如何在 ASP.NET MVC 中集成 AngularJS(2)
  3. eclipse中将Maven Dependencies Libraries移除后的恢复办法
  4. Azkaban 2.5.0 搭建
  5. Microsoft Enterprise Library 5.0 缓存配置
  6. SpringMVC实现一个controller里面有多个方法
  7. UITextView 监听 return key的改变
  8. 训练集(train set) 验证集(validation set) 测试集(test set)
  9. JDE910笔记2--OMW项目建立及简单使用
  10. ContentProvider URI的组成
  11. pidof,pgrep进程名查PID, /proc目录由pid查进程名
  12. Android fragment onActivityResult 不起作用
  13. LeetCode 之 Triangle
  14. Linux中的zero-copy技术
  15. Cocos2d中update与fixedUpdate的区别(二)
  16. Cesium 中两种添加 model 方法的区别
  17. Go开发之路 -- 函数详解
  18. 如何简单理解js中this的指向
  19. python 读 json 文件
  20. BZOJ4538 HNOI2016网络(树链剖分+线段树+堆/整体二分+树上差分)

热门文章

  1. Sybase数据库异常紧急恢复
  2. uCGUI窗口的创建过程分析
  3. DB天气app冲刺第二天
  4. js去除重复数值
  5. 使用Sass预定义一些常用的样式,非常方便(转)
  6. javascript和jquery动态创建html元素
  7. hdu 3717
  8. SecureCRT 中文乱码问题
  9. Design Tutorial: Inverse the Problem
  10. POJ2402+模拟