歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Windows及Linux平台下的計時函數總結

Windows及Linux平台下的計時函數總結

日期:2017/3/3 15:55:43   编辑:關於Linux

本文對Windows及Linux平台下常用的計時函數進行總結,包括精度為秒、毫秒、微秒三種精度的各種函數。

比如Window平台下特有的Windows API函數GetTickCount()、timeGetTime()、及QueryPerformanceCounter(),

Linux平台下特有的gettimeofday()函數,以及標准的C/C++函數time()和clock()。下面分別對此進行簡單介紹並附上示例代碼。

通用的C/C++計時函數time()和clock()

time_t time(time_t *timer);

返回以格林尼治時間(GMT)為標准,從1970年1月1日00:00:00到現在的此時此刻所經過的秒數。

time_t實際是個long長整型typedef long time_t;

clock_t clock(void);

返回進程啟動到調用函數時所經過的CPU時鐘計時單元(clock tick)數,在MSDN中稱之為掛鐘時間(wal-clock),以毫秒為單位。

clock_t實際是個long長整型typedef long clock_t;

Window平台特有函數

DWORD timeGetTime(void);

返回系統時間,以毫秒為單位。系統時間是從系統啟動到調用函數時所經過的毫秒數。注意,這個值是32位的,會在0到2^32之間循環,約49.71天。

DWORD WINAPI GetTickCount(void);

這個函數和timeGetTime()一樣也是返回系統時間,以毫秒為單位。

高精度計時,以微秒為單位(1毫秒=1000微秒)。

更多精彩內容:http://www.bianceng.cn/OS/Linux/

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);得到高精度計時器的值(如果存在這樣的計時器)。

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);返回硬件支持的高精度計數器的頻率(次每秒),返回0表示失敗。

其中LARGE_INTEGER其實是一個聯合體,可以得到__int64 QuadPart;也可以分別得到低32位DWORD LowPart和高32位的值LONG HighPart。

在使用時,先使用QueryPerformanceFrequency()得到計數器的頻率,再計算二次調用QueryPerformanceCounter()所得的計時器值之差,

用差去除以頻率就得到精確的計時了。

Linux平台特有函數

int gettimeofday(struct timeval *tv,struct timezone *tz);

獲得當前精確時間(1970年1月1日到現在的時間),精度為微秒。

保存時間的結構體

strut timeval {

long tv_sec; //秒數

long tv_usec; //微秒數

};

附上代碼

  1 #include <iostream>
  2 
  3 #if defined(_WIN32) || defined(WIN32)        /**Windows*/
  4 #define WINDOWS_IMPL
  5 #include <windows.h>
  6 #include <time.h>            //time() 、 clock()
  7 #include <Mmsystem.h>       //timeGetTime()
  8 #pragma comment(lib, "Winmm.lib") //timeGetTime()
  9 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(BSD)    /**Linux*/
 10 #define LINUX_IMPL
 11 #include <sys/time.h>        //gettimeofday()
 12 #endif
 13 #include <stdio.h>
 14 
 15 /***********************************************************
 16 通用的:
 17 time_t time(time_t *tloc);     //返回從1970年1月1日0點以來的秒數,精度為秒
 18 clock_t clock(): 返回該程序從啟動到函數調用占用CPU的時間,精度為毫秒,但一般最小精度是33ms
 19 
 20 Windows特有:
 21 GetTickCount(): 返回從操作系統啟動到現在所經過的毫秒數,精度毫秒,但最小精度是18ms
 22                 返回值以32位的雙字類型DWORD存儲,因此可以存儲的最大值是2^32 ms約為49.71天,
 23 timeGetTime():    返回以毫秒計的系統時間,該時間為從系統開啟算起所經過的時間,精度為毫秒
 24 QueryPerformanceCounter(): 返回高精確度性能計數器的值,精度為微妙,但是確切的精確計時的最小單位是與系統有關的
 25 
 26 Linux特有:
 27 gettimeofday(struct timeval *tv,struct timezone *tz); 獲得當前精確時間(1970年1月1日到現在的時間),精度為微秒
 28 ***********************************************************/
 29 
 30 void MySleep(int sec_time)
 31 {
 32     #if defined(WINDOWS_IMPL)
 33         Sleep(sec_time*1000);
 34     #elif defined(LINUX_IMPL)
 35         sleep(sec_time);
 36     #endif
 37 }
 38 
 39 void test_time()
 40 {
 41     //通用的
 42     //用time()來計時  秒
 43     time_t timeBegin, timeEnd;
 44     timeBegin = time(NULL);
 45     MySleep(1);
 46     timeEnd = time(NULL);
 47     printf("%d\n", timeEnd - timeBegin);
 48 
 49     /*
 50      * Structure used in select() call, taken from the BSD file sys/time.h.
 51      */
 52     //struct timeval {
 53     //        long    tv_sec;         /* seconds */
 54     //        long    tv_usec;        /* and microseconds */
 55     //};
 56     timeval  val;
 57 
 58     //用clock()來計時  毫秒
 59     clock_t  clockBegin, clockEnd;
 60     clockBegin = clock();
 61     MySleep(1);
 62     clockEnd = clock();
 63     printf("%d\n", clockEnd - clockBegin);
 64 
 65 #ifdef WINDOWS_IMPL
 66     //Windows
 67 
 68     //用GetTickCount()來計時  毫秒
 69     DWORD  dwGTCBegin, dwGTCEnd;
 70     dwGTCBegin = GetTickCount();
 71     Sleep(1000);
 72     dwGTCEnd = GetTickCount();
 73     printf("%d\n", dwGTCEnd - dwGTCBegin);
 74 
 75     //用timeGetTime()來計時  毫秒
 76     DWORD  dwBegin, dwEnd;
 77     dwBegin = timeGetTime();
 78     Sleep(1000);
 79     dwEnd = timeGetTime();
 80     printf("%d\n", dwEnd - dwBegin);
 81     
 82     //用QueryPerformanceCounter()來計時  微秒
 83     LARGE_INTEGER  large_interger;
 84     double dff;
 85     __int64  c1, c2;
 86     QueryPerformanceFrequency(&large_interger);
 87     dff = large_interger.QuadPart;
 88     QueryPerformanceCounter(&large_interger);
 89     c1 = large_interger.QuadPart;
 90     Sleep(1000);
 91     QueryPerformanceCounter(&large_interger);
 92     c2 = large_interger.QuadPart;
 93     printf("高精度計時器頻率%lf\n", dff);
 94     printf("第一次計時器值%I64d 第二次計時器值%I64d 計時器差%I64d\n", c1, c2, c2 - c1);
 95     printf("計時%lf毫秒\n", (c2 - c1) * 1000 / dff);
 96 
 97 #elif  defined(LINUX_IMPL)
 98     //Linux
 99 
100     struct timeval tpstart,tpend;
101     double timeuse;
102     gettimeofday(&tpstart,NULL);
103     sleep(1);
104     gettimeofday(&tpend,NULL);
105     timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;//注意,秒的讀數和微秒的讀數都應計算在內
106     printf("used time:%fus\n",timeuse);
107 #endif
108 
109 }
110 
111 int main()
112 {
113     test_time();
114         getchar();
115     return 0;
116 }

在Windows平台下運行結果如下:

在Linux平台下運行結果如下:

作者:cnblogs lizhenghn

Copyright © Linux教程網 All Rights Reserved