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 ...
随机推荐
- win7 IIS7 PHP环境配置
PHP5.2.17 官方下载: http://windows.php.net/downloads/releases/php-5.2.17-Win32-VC6-x86.zip PHP5.3.5 官方下载 ...
- .NET设计模式(5):工厂方法模式(Factory Method)(转)
工厂方法模式(Factory Method) ——.NET设计模式系列之五 Terrylee,2004年1月2日 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实 ...
- [Bug]没有对“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”的写访问权限
问题 环境WIN8.1 x64,新安装的vs与iis,在部署网站时,出现该异常信息.本地机作为测试机,就部署一个站点进行测试,没想到出现这个错误. 解决方案 如果你访问上面的那个路径,你会发现并没有那 ...
- 浅谈MySQL索引背后的数据结构及算法【转】
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- Sqli-labs less 16
Less-16 本关我们的处理方法和less15是一样的,同样的使用延时注入的方法进行解决.这里直接从源代码中看到对id进行 ("id")的处理.(请自行测试) 提交的payloa ...
- 获取及管理Android 手机运营商及状态
主要类 TelephonyManager: telephonyManager.getCellLocation();//获得服务区 telephonyManager.getCellId();//获得服务 ...
- MySQL5.6 Replication主从复制(读写分离) 配置完整版
MySQL5.6 Replication主从复制(读写分离) 配置完整版 MySQL5.6主从复制(读写分离)教程 1.MySQL5.6开始主从复制有两种方式: 基于日志(binlog): 基于GTI ...
- 以Server模式启动Derby服务竟然抛套接字权限异常
以Server模式启动Derby服务竟然抛套接字权限异常:access denied ("java.net.SocketPermission" "localhost:15 ...
- lintcode :最长公共前缀
题目 最长公共前缀 给k个字符串,求出他们的最长公共前缀(LCP) 样例 在 "ABCD" "ABEF" 和 "ACEF" 中, LCP ...
- Emmet语法介绍
例子: (div+p#test>span.test2.test3)*5+p[name="hello"]>div.test4^a*5 <div></di ...