(转)windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime
执行 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;
}
(转)windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime的更多相关文章
- windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime
http://gmd20.blog.163.com/blog/static/168439232012113111759514/ 执行 10000000 次, 耗时 2258,369 微秒 Qu ...
- 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平台下常用的计时函数 ...
- C++程序在Windows平台上各种定位内存泄漏的方法,并对比了它们的优缺点
一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...
- Windows平台分布式架构实践 - 负载均衡
概述 最近.NET的世界开始闹腾了,微软官方终于加入到了对.NET跨平台的支持,并且在不久的将来,我们在VS里面写的代码可能就可以通过Mono直接在Linux和Mac上运行.那么大家(开发者和企业)为 ...
- C++中的时间函数
C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...
随机推荐
- linux shell 删除指定文件夹下面 名称不包含指定字符的文件
find /app/jenkins/jenkins/jobs/scam/* ! -name config.xml | xargs rm -rf 删除/app/jenkins/jenkins/jobs/ ...
- django1.8输出一些非HTML内容
在reportlab库中可以生成pdf文件 在https://www.reportlab.com/pypi/packages/ 下载需要的版本然后,在命令行里通过pip安装.pip instal ...
- spineRuntTime for cocos2dx v3,attack播完后回到idle
spineRuntTime for cocos2dx v3,attack播完后回到idle. _animationNode = spine::SkeletonAnimation::createWith ...
- 成员函数的重载&&隐藏&&覆盖
/* *成员函数的重载,覆盖,隐藏 *重载: *1.同样的范围(在同一个类中) *2.函数名同样 *3.參数不同 *4.virtualkeyword可有可无 *覆盖是指派生类覆盖基类的函数,特征是: ...
- 安卓getSystemService
getSystemService是Activity中的方法,依据传入的name来取得相应的服务对象,这些服务名称參数都是Context类中的常量 Name ...
- Linux下C++连MySQL数据库
1.查看本地有没有安装mysql,命令就是mysql,如果有这个命令就表示安装了mysql数据库软件.如果没有就自行安装. MySQL-server-4.0.16-0.i386.rpm MySQL-c ...
- Chisel Tutorial(七)——模块
下面内容根据2015-7-10版的Chisel 2.2 Tutorial整理 Chisel中的模块与Verilog HDL中模块的概念十分相似,都是用层次结构描写叙述电路.Chisel中的module ...
- C++面向对象程序设计的一些知识点(5)
摘要:运算符能给程序员提供一种书写数学公式的感觉,本质上运算符也是一种函数,因此有类内部运算符和全局运算符之分,通过重载,运算符的“动作”更加有针对性,编写代码更像写英文文章. 1.C++标准允许将运 ...
- ehcache OR Memcache
ehcache是纯java编写的,通信是通过RMI方式,适用于基于java技术的项目.memcached服务器端是c编写的,客户端有多个语言的实现,如c,php(淘宝,sina等各大门户网站),pyt ...
- dp之分组背包hdu3535(推荐)
题意:有0,1,2三种任务,0任务中的任务至少得完成一件,1中的任务最多完成1件,2中的任务随便做.每一个任务最多只能做一次 .n代表有n组任务,t代表有t分钟,m代表这组任务有m个子任务,s代表这m ...