Executes 10 million times and takes 2258,369 microseconds QueryPerformanceCounter
Executes 10 million times and takes 26,347 microseconds GetTickCount
Executes 10 million times, takes 242,879 microseconds time ()
Time function of C (time_t) is about 6 times times slower than getsystemtimeasfiletime, 6 times times faster than _ftime.
Executes 10 million times and takes 1310,066 microseconds _ftime
Executes 10 million times and takes 1722,125 microseconds Getlocaltime
Executes 10 million times and takes 39,131 microseconds Getsystemtimeasfiletime
Getlocaltime time-consuming equals = getsystemtimeasfiletime time-consuming + filetimetosystemtime time-consuming
------------
You can see that the higher the accuracy, the worse
GetTickCount accuracy 1 ms > Getlocaltime Accuracy 100 nanoseconds (0.1 microseconds) > QueryPerformanceCounter (I don't know how this is so bad)
If you only want to calculate the time skew, you can use the Getsystemtimeasfiletime, which can reach 100 nanoseconds,
MSDN has an introduction.
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's not recommended, you add, and subtract values from the FILETIME structure to obtain relative times. Instead, should copy the Low-and high-order parts of the file time to a ularge_integer structure, perform 64-bit Arit Hmetic on the QuadPart member, and copy the LowPart and Highpart members into the FILETIME structure.
Don't 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.
The test code is as follows
#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;
}
Transferred from: http://gmd20.blog.163.com/blog/static/168439232012113111759514/
(turn) Windows platform Time function performance comparison Queryperformancecounter,gettickcount,ftime,time,getlocaltime,getsystemtimeasfiletime