执行 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 (intchar**)
{
   
 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(&lt); 
    //total_counter += lt.wMilliseconds;
    
    // total_counter += _time32(NULL);   time(NULL)
   
     GetSystemTimeAsFileTime(&SystemTimeAsFileTime);
     FileTimeToSystemTime(&SystemTimeAsFileTime,&lt);
     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;
}
 
 转自:http://gmd20.blog.163.com/blog/static/168439232012113111759514/

(转)windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime的更多相关文章

  1. windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime

    http://gmd20.blog.163.com/blog/static/168439232012113111759514/ 执行 10000000 次, 耗时 2258,369 微秒     Qu ...

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

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

  3. Windows获取时间函数(使用GetLocalTime,GetSystemTime,SystemTimeToTzSpecificLocalTime,GetFileTime API函数

    获取本地时间 typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; ...

  4. windows时间函数

    介绍        我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执 行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记 ...

  5. Windows 各种计时函数总结

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

  6. <转>Windows 各种计时函数总结

    本文转自MoreWindows 特此标识感谢 http://blog.csdn.net/morewindows/article/details/6854764 本文对Windows平台下常用的计时函数 ...

  7. C++程序在Windows平台上各种定位内存泄漏的方法,并对比了它们的优缺点

    一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...

  8. Windows平台分布式架构实践 - 负载均衡

    概述 最近.NET的世界开始闹腾了,微软官方终于加入到了对.NET跨平台的支持,并且在不久的将来,我们在VS里面写的代码可能就可以通过Mono直接在Linux和Mac上运行.那么大家(开发者和企业)为 ...

  9. C++中的时间函数

    C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...

随机推荐

  1. 后缀crt证书转换

    转换地址:https://www.chinassl.net/ssltools/convert-ssl.html 转换成功后点下载即可

  2. 小半斤拔凉 支付Java 相关参考

    http://git.oschina.net/littleCrazy/dianshangpingtai-zhifu http://git.oschina.net/52itstyle/springMvc ...

  3. django中cookies和session

    django中cookies和session是两个经常使用的用户认证工具.都是类似于字典的数据类型,都是request的内部属性 cookies的读写方法 cookies读,比如username us ...

  4. Python shell对比

    对Python.shell的一些思考 如果使用python去写脚本来处理日常事务的话,相对于shell是一件比较麻烦的事情,因为我可以使用shell在花费更少的时间内,比较熟练地使用awk.sed和g ...

  5. jQuery $.extend()使用方法

    $.extend()使用方法总结. jQuery为开发插件提拱了两个方法,各自是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.ex ...

  6. Tomcat Jboss Glassfish 三种常见web容器比较

    一.缘由: 新公司平台是纯Java架构,有用到Java Web(JSP).Java 业务(EJB).Nginx..Websphere MQ.Mysql这样.大家知道java是跑在容器里的, 这里的业务 ...

  7. C# 获取指定目录下所有文件信息

    /// <summary> /// 返回指定目录下所有文件信息 /// </summary> /// <param name="strDirectory&quo ...

  8. [转帖]cocos2D-X源码分析之从cocos2D-X学习OpenGL(3)----BATCH_COMMAND

    原贴: cocos2D-X源码分析之从cocos2D-X学习OpenGL(3)----BATCH_COMMAND 上一篇介绍了QUAD_COMMAND渲染命令,顺带介绍了VAO和VBO,这一篇介绍批处 ...

  9. Win7  CMD大全

    Win7  CMD大全  compmgmt.msc---计算机管理 conf—-启动 netmeeting charmap–-启动字符映射表 calc—-启动计算器 chkdsk.exe–-Chkds ...

  10. 【Unity】UGUI的Text各种小问题

    Text:用中文输入法时,无法输入汉字.输入了拼音后,按回车键无反应.目前的办法是在别的地方打好字后复制过来. Font:字体必须选一个,选None则文字变成一串黑色方块. Font Size:文字大 ...