windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTim
2016-06-03 21:35
513 查看
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.
测试代码如下
c语言精确到微妙 GetSystemTimeAsFileTime
c语言库函数中的clock()函数只能精确到ms,若想更精确的us,在网络上查了一遍,完整的可行的解决方案繁琐,实在没时间去仔细琢磨。不过找到了一个简洁的方案:调用GetSystemTimeAsFileTime函数,单位是100ns。
时间的单位换算 :
1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)
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,0
执行 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秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)
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,0
相关文章推荐
- 如何重装TCP/IP协议
- Windows 8 官方高清壁纸欣赏与下载
- 谁是桌面王者?Win PK Linux三大镇山之宝
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Windows Clang开发环境备忘
- 从Windows系统下访问Linux分区相关软件
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Windows下搭建本地SVN服务器
- 使用Windows原生命令一键清空剪贴板
- windows用windeployqt发布qt quick application程序
- 利用开源软件打造自己的全功能远程工具
- Windows 8虚拟机不能全屏的解决方法
- Visual Studio 2012 示例代码浏览器 - 数以千计的开发示例近在手边,唾手可得
- Visual Studio 2012 示例代码浏览器 - 数以千计的开发示例近在手边,唾手可得
- 微软镜像下载
- windows server域用户提升到本地更高权限组中的方法
- 使用命令修改注册表键值及权限