獲取系統時間,精確到微秒

#include <stdio.h>
#include <sys/time.h>php

int main() {
    struct timeval start, end;
    gettimeofday( &start, NULL );
    sleep(3);
    gettimeofday( &end, NULL );
    int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
    printf("time: %d us\n", timeuse);
    return 0;
}java


實驗結果:windows

在作測試或性能優化時,常常要知道程序運行的時間,在Linux系統能夠使用time命令來計算程序運行運行所消耗的時間,能精確到毫秒,若是要精 確到代碼塊或某個操做運行時所消耗的時間,time命令就不給力了。若是對時間的精度要求不高的話,能夠調用標準C的接口time來獲得開始和結束的時 間,再調用difftime接口來計算時間差,精度是秒,代碼以下所示:性能優化

下載: time.c性能

  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int main(){
  5.     time_t t_start, t_end;
  6.     t_start = time(NULL) ;
  7.     sleep(3000);
  8.     t_end = time(NULL) ;
  9.     printf("time: %.0f s\n", difftime(t_end,t_start)) ;
  10.     return 0;
  11. }

若是要讓程序休眠3秒,Windows使用Sleep(3000),Linux使用sleep(3),即Windows的Sleep接口的參數的單位是毫秒,Linux的sleep接口的參數的單位是秒。

若是須要精確到毫秒,以上程序就發揮不了做用,若是在Java要達到這要求就很簡單了,代碼以下所示:測試

下載: Time.java優化

  1. public class Time {
  2.     public static void main(String[] args) {
  3.         try {
  4.             long startTime = System.currentTimeMillis();
  5.             Thread.sleep(3000);
  6.             long endTime = System.currentTimeMillis();
  7.             System.out.println("time: " + (endTime - startTime) + " ms");
  8.         } catch (InterruptedException e) {
  9.             e.printStackTrace();
  10.         }
  11.     }
  12. }

經過Google找了一些資料後,發現C語言裏沒有標準的接口能夠得到精確到毫秒的時間,都會調用到與操做系統相關的API,下面會分別介紹在Linux和Windows系統下的多種實現方法,但願對你們有幫助。spa

使用gettimeofday接口:操作系統

下載: gettimeofday.ccode

  1. #include <stdio.h>
  2. #include <sys/time.h>
  3.  
  4. int main() {
  5.     struct timeval start, end;
  6.     gettimeofday( &start, NULL );
  7.     sleep(3);
  8.     gettimeofday( &end, NULL );
  9.     int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
  10.     printf("time: %d us\n", timeuse);
  11.     return 0;
  12. }

gettimeofday能獲得微秒數,比毫秒還要更精確。

使用ftime接口:

下載: ftime.c

  1. #include <stdio.h>
  2. #include <sys/timeb.h>
  3.  
  4. long long getSystemTime() {
  5.     struct timeb t;
  6.     ftime(&t);
  7.     return 1000 * t.time + t.millitm;
  8. }
  9.  
  10. int main() {
  11.     long long start=getSystemTime();
  12.     sleep(3);
  13.     long long end=getSystemTime();
  14.  
  15.     printf("time: %lld ms\n", end-start);
  16.     return 0;
  17. }

Windows系統

使用GetTickCount接口:

下載: GetTickCount.c

  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main() {
  5.     DWORD start, stop;
  6.     start = GetTickCount();
  7.     Sleep(3000);
  8.     stop = GetTickCount();
  9.     printf("time: %lld ms\n", stop - start);
  10.     return 0;
  11. }

Windows系統下有些編譯器使用printf輸出64位整數參數要使用%I64d,好比VC。

使用QueryPerformanceX接口:

下載: QueryPerformance.c

  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main(){
  5.     LARGE_INTEGER li;
  6.     LONGLONG start, end, freq;
  7.     QueryPerformanceFrequency(&li);
  8.     freq = li.QuadPart;
  9.     QueryPerformanceCounter(&li);
  10.     start = li.QuadPart;
  11.     Sleep(3000);
  12.     QueryPerformanceCounter(&li);
  13.     end = li.QuadPart;
  14.     int useTime =(int)((end - start) * 1000 / freq);
  15.     printf("time: %d ms\n", useTime);
  16.     return 0;
  17. }

使用GetSystemTime接口:

下載: GetSystemTime.c

  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main(){
  5.     SYSTEMTIME currentTime;
  6.     GetSystemTime(&currentTime);
  7.     printf("time: %u/%u/%u %u:%u:%u:%u %d\n",           
  8.      currentTime.wYear,currentTime.wMonth,currentTime.wDay,
  9.      currentTime.wHour,currentTime.wMinute,currentTime.wSecond,
  10.      currentTime.wMilliseconds,currentTime.wDayOfWeek);
  11.     return 0;
  12. }

這種方法沒給出計算時間差的實現,只給出如何用GetSystemTime調用獲得當前時間,計算時間差比較簡單,根據年、月、日、時、分秒和毫秒計算出一個整數,再將兩整數相減便可。

後記

以上是經過Google找到一些用C語言得到精確到毫秒的實現方法,對比Linux和Windows的方法,發現兩個系統的API命名很不同,Linux接口名要麼都是小寫要麼使用下劃線(_)來分隔單詞,而Windows接口名中的單詞首字母大寫。

相關文章
相關標籤/搜索