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. JavaScript之常用方法讲解

    1.indexOf() 定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(searchvalue,frominde ...

  2. JavaScript之Throw、Try 、Catch讲解

    try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. 错误一定会发生 当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错误: 可能是 ...

  3. DSP中的cmd文件

    一.CMD文件 链接命令文件(Link Command Files),以后缀.cmd结尾,简称CMD文件. CMD文件的两大功能是指示存储空间和分配段到存储空间. 在编写CMD文件时,主要采用MEMO ...

  4. HDU 2529 Shot (物理数学题)

    题目 解题过程: //物理数学题 #include<stdio.h> #include<string.h> #include<algorithm> using na ...

  5. 就地交叉数组元素[a1a2b1b2]->[a1b1a2b2]

    问题描述: If [a1,a2,a3...,an,b1,b2...bn] is given input change this to [a1,b1,a2,b2.....an,bn] , solutio ...

  6. 牛 JQuery视频笔记

    QX3GLL 包装集 next() nextAll() nextAll("div"); prev();prevAll() end();andSlf(); eq(2);gt(1);l ...

  7. Mac Air maven 环境配置

    mave 的配置 检出项目遇到问题: Could not calculate build plan: Failure to transfer org.apache.maven.plugins:mave ...

  8. 网站建设底层知识Socket与Http解析

    在进行网站建设的时候,常常遇到不同的协议,Socket和http协议都可以实现数据传输,但两种传输方式在网站建设中有什么各自的特点,和缺点,如何选择合适的传输方式. 1 数据传输方式 1.1  Soc ...

  9. Linux网络编程4——个人总结

    TCP与UDP通信流程 TCP通信的基本步骤如下: 服务端:socket---bind---listen---while(1){---accept---recv---send---close---}- ...

  10. ngrok本地反向代理

    ngrok本地反向代理 ngrok本地反向代理 使用ngrok可以把内网服务映射到外网 国内ngrok服务配置如下 在ngrok.exe所在的目录下添加ngrok.cfg文件 ngrok.cfg文件内 ...