windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime
http://gmd20.blog.163.com/blog/static/168439232012113111759514/
执行 10000000 次, 耗时 2258,369 微秒 QueryPerformanceCounter
执行 10000000 次, 耗时 26,347 微秒 GetTickCount
执行 10000000 次, 耗时 242,879 微秒 time()
c的时间函数 time(time_t) 大概比GetSystemTimeAsFileTime慢6倍,比_ftime 快6倍
执行 10000000 次, 耗时 1310,066 微秒 _ftime
执行 10000000 次, 耗时 1722,125 微秒 GetLocalTime
执行 10000000 次, 耗时 39,131 微秒 GetSystemTimeAsFileTime
GetLocalTime耗时等于 = GetSystemTimeAsFileTime 耗时+ FileTimeToSystemTime 的耗时
------------
可以看到精度越高性能越差
GetTickCount 精度1毫秒 > GetLocalTime 精度100纳秒 (0.1 微秒) > QueryPerformanceCounter (搞不懂这个怎么这么差)
如果仅仅为了计算时间偏差,可以使用 GetSystemTimeAsFileTime,这个精度可以达到100纳秒,
msdn有个介绍。
http://msdn.microsoft.com/ZH-CN/library/windows/desktop/ms724284(v=vs.85).aspx
Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
It is not recommended that you add and subtract values from the FILETIME structure to obtain relative times. Instead, you should copy the low- and high-order parts of the file time to a ULARGE_INTEGER structure, perform 64-bit arithmetic on the QuadPart member, and copy the LowPart and HighPart members into the FILETIME structure.
Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER* or __int64* value because it can cause alignment faults on 64-bit Windows.
测试代码如下
#include <iomanip>#include <fstream>#include <iostream>#include <map>#include <sstream>#include <list>#include <vector>#include <stdlib.h>#include <stdint.h>#include <stdio.h>#include <sys/types.h>#include <sys/timeb.h>#include <time.h>#include <Windows.h>#include "Trace.h" using namespace std; int main (int, char**){ LARGE_INTEGER freq, t0, t1; QueryPerformanceFrequency(&freq); size_t number = 10000000; int total_counter = 0; //LARGE_INTEGER t3; //struct timeb timebuffer; SYSTEMTIME lt; FILETIME SystemTimeAsFileTime; QueryPerformanceCounter(&t0); for (int i=0; i< number; i++) { //QueryPerformanceCounter(&t3); //total_counter += t3.LowPart; //total_counter += GetTickCount(); //ftime(&timebuffer); //total_counter += timebuffer.time; //GetLocalTime(<); //total_counter += lt.wMilliseconds; // total_counter += _time32(NULL); time(NULL) GetSystemTimeAsFileTime(&SystemTimeAsFileTime); FileTimeToSystemTime(&SystemTimeAsFileTime,<); total_counter += lt.wMilliseconds; } QueryPerformanceCounter(&t1); int time = (((t1.QuadPart-t0.QuadPart)*1000000)/freq.QuadPart); std::cout << "执行 " << number <<" 次, 耗时 " << time << " 微秒" << std::endl; std::cout << total_counter; int a; cin >> a; return 0;}c语言精确到微妙 GetSystemTimeAsFileTime
c语言库函数中的clock()函数只能精确到ms,若想更精确的us,在网络上查了一遍,完整的可行的解决方案繁琐,实在没时间去仔细琢磨。不过找到了一个简洁的方案:调用GetSystemTimeAsFileTime函数,单位是100ns。
1秒=1,000,000 微秒(μs) 1微秒=1/1,000,000秒(s)
1秒=1,000,000,000 纳秒(ns) 1纳秒=1/1,000,000,000秒(s)
1秒=1,000,000,000,000 皮秒(ps) 1皮秒=1/1,000,000,000,000秒(s)
windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime的更多相关文章
- (转)windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime
执行 10000000 次, 耗时 2258,369 微秒 QueryPerformanceCounter 执行 10000000 次, 耗时 26,347 微秒 GetTickCoun ...
- Windows 各种计时函数总结(QueryPerformanceCounter可以达到微秒)
本文对Windows平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的5种方法.分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock ...
- Windows获取时间函数(使用GetLocalTime,GetSystemTime,SystemTimeToTzSpecificLocalTime,GetFileTime API函数
获取本地时间 typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; ...
- windows时间函数
介绍 我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执 行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记 ...
- Windows 各种计时函数总结
本文对Windows平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的 5种方法.分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及cloc ...
- <转>Windows 各种计时函数总结
本文转自MoreWindows 特此标识感谢 http://blog.csdn.net/morewindows/article/details/6854764 本文对Windows平台下常用的计时函数 ...
- Windows高精度时间
目录 第1章计时 1 1.1 GetTickCount 1 1.2 timeGetTime 1 1.3 QueryPerformanceCounter 1 1.4 测试 ...
- windows获取时间的方法
介绍 我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执 行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记录 ...
- C++程序在Windows平台上各种定位内存泄漏的方法,并对比了它们的优缺点
一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...
随机推荐
- Spring.net Could not load type from string value
最近有点懒了啊,都没有按时上来博客园更新下,个人觉得遇到难题的时候在这里留下脚印也亦造福他人,进来 晓镜水月 被项目围的团团转,asp.net MVC项目来的,但是我还是不务正业啊,在弄网络爬虫,这个 ...
- linux下解压命令大全(转载)
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...
- javascript 关于函数的返回值
在javascript中根据调用方式的不同返回的内容也不同 1. 以函数的形式调用 当以函数的形式调用时, 返回值和函数定义时的 ruturn 有关, return的是数字就number类型, re ...
- 在linux服务器上装svn版本管理,自动部署代码到项目
在linux服务器上装svn版本管理,自动部署代码到项目 http://bbs.aliyun.com/read/9715.html?spm=5176.7114037.1996646101.1.W3zw ...
- mysql 中的bool值
boolean在MySQL里的类型为tinyint(1) 很奇怪.
- Linux目录结构【转】
使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系统,很久没有发表博文了 近期会整理自己所用所了解知识点,发表linux相关的 ...
- 性能标准:Apdex介绍
目前的应用程序性能测试工具有多方面的局限.每种工具都有其自己对性能的定义,产生太多混乱或矛盾的数值,并缺少简洁的结论.IT经理们无法深入了解性能,而这是软件与用户对关键应用的体验有关的事. 听云作为新 ...
- iOS开发swift--函数
函数返回值 ---函数 // 自加函数 func Add(num : Int)->Int{ } //自减函数 func zj(num : Int)->Int{ } //定义返回函数类型 f ...
- 安装MySQldb出错解决方法
sudo yum install mysql-devel sudo yum install python-devel _mysql.c:36:23: error: my_config.h: No su ...
- Centos环境下部署游戏服务器-常用命令
图1 在Linux的世界,如果你不玩命令,那你见了同行都不好意思和人家打招呼.同时服务器正常状况下放在远端,一般都是开ssh登录服务器,相信远程桌面的人很少见吧.这篇文章说说Linu ...