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 (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;
}
 

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,000秒(s)
 

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

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

    执行 10000000 次, 耗时 2258,369 微秒     QueryPerformanceCounter 执行 10000000 次, 耗时 26,347 微秒    GetTickCoun ...

  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. Windows高精度时间

    目录 第1章计时    1 1.1 GetTickCount    1 1.2 timeGetTime    1 1.3 QueryPerformanceCounter    1 1.4 测试     ...

  8. windows获取时间的方法

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

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

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

随机推荐

  1. 使用JFinal-weixin配置微信开发

    先扯点闲话,申请好公众号后,一直因为没有域名.没有外网IP而没有尝试过开发微信服务.后来发现nat123可以从内网直接映射到外网,但是nat123需要钱或者T币大于0,于是为了赚一个T币,签到灌水了一 ...

  2. [百度空间] [原]CImageList支持32位透明位图

    32位的位图主要是包含Alpha值(0-255)可以有半透效果的.之前用FreeImage加载 的DIB, CImageList直接绘制会有黑色背景.即便用了ILC_MASK,也创建了mask map ...

  3. 使用Visual Studio 2012 开发 Html5 应用

    Visual Studio 一直以来是开发微软旗下应用的利器,只要是开发微软相关的应用无论是Windows程序,WPF,Asp.Net,WinRT Surface,WindowsPhone 等微软旗下 ...

  4. 请教DotNetBar控件中的CalendarView控件如何拖动当前的时间轴

    本人想拖动那个当前的时间轴或者让时间轴变动,因为那个时间轴默认的是当前时间.(就是那个黄色的线)

  5. vc++ 获取当前用户名

    #include<afxwin.h> #include <stdio.h> int main(void) { char userName[MAX_PATH]; unsigned ...

  6. POJ 2185 Milking Grid (KMP,求最小覆盖子矩阵,好题)

    题意:给出一个大矩阵,求最小覆盖矩阵,大矩阵可由这个小矩阵拼成.(就如同拼磁砖,允许最后有残缺) 正确解法的参考链接:http://poj.org/showmessage?message_id=153 ...

  7. C Primer Plus之结构和其他数据形式

    声明和初始化结构指针 声明结构化指针,例如: struct guy * him; 初始化结构指针(如果barney是一个guy类型的结构),例如: him = &barney; 注意:和数组不 ...

  8. Linux:-bash: ***: command not found

    Linux:-bash: ***: command not found,系统很多命令都用不了,均提示没有此命令. 突然之间linux很多命令都用不了,均提示没有此命令. 这应该是系统环境变量出现了问题 ...

  9. PushBackInputStream与PushBackInputStreamReader的用法

    举个例子:获取XX内容 PushBackInputStream pb=new PushBackInputStream(in,4);//4制定缓冲区大小 byte[] buf=new byte[4]; ...

  10. hive-学习笔记

    1.hive模糊搜索表  show tables like '*name*'; 2.查看表结构信息  desc formatted table_name;  desc table_name; 3.查看 ...