(轉)windows平台時間函數效能比較QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime

來源:互聯網
上載者:User

標籤:

執行 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(&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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.