linux、windows 时间函数性能比较

来源:互联网 发布:学为贵封闭班 知乎 编辑:程序博客网 时间:2024/06/08 05:16

GetTickCount在Linux下的实现方法、

// 返回自系统开机以来的毫秒数(tick)
unsigned long GetTickCount()
{
  struct timeval tv;
  if( gettimeofday(&tv, NULL) != 0 )
    return 0;
 
  return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}

// 返回自系统开机以来的毫秒数(tick)
unsigned long GetTickCount()
{
    struct timespec ts;

     clock_gettime(CLOCK_MONOTONIC, &ts);

     return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}

附: time()、gettimeofday()及GetTickCount()效率比较 time()和gettimeofday()是linux的系统调用,GetTickCount()是windows的函数。

time:返回至1970-1-1 00:00:00至今的秒数; gettimeofday:除了返回秒数外,还有纳秒数,用于更精确的计时; GetTickCount:返回系统启动至今的毫秒数

使用三个函数,分别调用1千万次,测试结果如下: time:约967毫秒; gettimeofday:约1800毫秒; GetTickCount:约70毫秒;

可见,GetTickCount要快很多很多,在linux系统下,频繁使用time及gettimeofday严重影响效率。


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

执行 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;
}
 


0 0