本文转自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 ;
}

<转>Windows 各种计时函数总结的更多相关文章

  1. Windows 各种计时函数总结

    本文对Windows平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的 5种方法.分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及cloc ...

  2. Windows 各种计时函数总结(QueryPerformanceCounter可以达到微秒)

    本文对Windows平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的5种方法.分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock ...

  3. Windows及Linux平台下的计时函数总结

    本文对Windows及Linux平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的各种函数.比如Window平台下特有的Windows API函数GetTickCount().timeG ...

  4. Linux下clock计时函数学习【转】

    转自:https://www.cnblogs.com/wfwenchao/p/5195022.html 平时在Linux和Winows下都有编码的时候,移植代码的时候免不了发现一些问题.1. 你到底准 ...

  5. Linux下clock计时函数学习

    平时在Linux和Winows下都有编码的时候,移植代码的时候免不了发现一些问题.1. 你到底准不准?关于clock()计时函数首先是一段简单的测试代码,功能为测试从文本文件读取数据并赋值给向量最后打 ...

  6. c中计时函数 clock()

    #include<time.h> int main() { // ... .. // .... printf("Time used = %.2lf\n",(double ...

  7. 【C/C++】计时函数比较

    目前,存在着各种计时函数,一般的处理都是先调用计时函数,记下当前时间tstart,然后处理一段程序,再调用计时函数,记下处理后的时间tend,再tend和tstart做差,就可以得到程序的执行时间,但 ...

  8. c++ windows下计时

    多核时代不宜再用 x86 的 RDTSC 指令测试指令周期和时间 陈硕Blog.csdn.net/Solstice 自从 Intel Pentium 加入 RDTSC 指令以来,这条指令是 micro ...

  9. mfc 调用Windows的API函数实现同步异步串口通信(源码)

    在工业控制中,工控机(一般都基于Windows平台)经常需要与智能仪表通过串口进行通信.串口通信方便易行,应用广泛. 一般情况下,工控机和各智能仪表通过RS485总线进行通信.RS485的通信方式是半 ...

随机推荐

  1. Oracle目录结构及创建新数据库

    oracle目录结构 当需要创建新的数据仓库时我可以用 Database Configuration Assistant(数据库配置助手) admin 存放创建的不同数据库 cfgtoollogs c ...

  2. MyEclipse启动和运行速度优化

    1:去除不需要加载的模块 Windows – Preferences - General - Startup and Shutdown,这个时候在右侧就显示出了Eclipse启动时加载的模块,可以根据 ...

  3. 利用百度地图API,在浏览器中找到自己的位置

    首先你得有个百度地图的秘钥,http://lbsyun.baidu.com/apiconsole/key 剩下的就是编码了 这里面会用到一个javascript里的一个函数,getMyLocation ...

  4. Windows 2008 故障转移群集介绍

    转载:http://dufei.blog.51cto.com/382644/902026 今天有客户问起Windows 群集的相关内容,毕竟Windows Server2008所支持的群集技术和Win ...

  5. How to Run Node.js with Express on Mobile Devices

    We released a JXcore plugin for Apache Cordova recently and in this article I will show how to run a ...

  6. POJ 3159 Candies(差分约束)

    http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...

  7. Vmware 8.00 文件共享ubuntu

    http://bolg.sinaapp.com/html/2012/1848.html 这是解决vm不能共享的解决方案. 今天学会的Linux命令: cp -i *** ~/tmp cd VMware ...

  8. 使用div+css制作简单导航 以及要注意问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. nginx 配置的server_name参数(转)

    转自:http://www.sklinux.com/373 nginx中的server_name指令主要用于配置基于名称虚拟主机. 一 匹配顺序,server_name指令在接到请求后的匹配顺序如下: ...

  10. http://blog.csdn.net/bluejoe2000/article/details/39521405#t9

    http://blog.csdn.net/bluejoe2000/article/details/39521405#t9