#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
![](http://static.javashuo.com/static/loading.gif)
在作測試或性能優化時,常常要知道程序運行的時間,在Linux系統能夠使用time命令來計算程序運行運行所消耗的時間,能精確到毫秒,若是要精 確到代碼塊或某個操做運行時所消耗的時間,time命令就不給力了。若是對時間的精度要求不高的話,能夠調用標準C的接口time來獲得開始和結束的時 間,再調用difftime接口來計算時間差,精度是秒,代碼以下所示:性能優化
下載: time.c性能
- #include <stdio.h>
- #include <time.h>
-
- int main(){
- time_t t_start, t_end;
- t_start = time(NULL) ;
- sleep(3000);
- t_end = time(NULL) ;
- printf("time: %.0f s\n", difftime(t_end,t_start)) ;
- return 0;
- }
若是要讓程序休眠3秒,Windows使用Sleep(3000),Linux使用sleep(3),即Windows的Sleep接口的參數的單位是毫秒,Linux的sleep接口的參數的單位是秒。
若是須要精確到毫秒,以上程序就發揮不了做用,若是在Java要達到這要求就很簡單了,代碼以下所示:測試
下載: Time.java優化
- public class Time {
- public static void main(String[] args) {
- try {
- long startTime = System.currentTimeMillis();
- Thread.sleep(3000);
- long endTime = System.currentTimeMillis();
- System.out.println("time: " + (endTime - startTime) + " ms");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
經過Google找了一些資料後,發現C語言裏沒有標準的接口能夠得到精確到毫秒的時間,都會調用到與操做系統相關的API,下面會分別介紹在Linux和Windows系統下的多種實現方法,但願對你們有幫助。spa
使用gettimeofday接口:操作系統
下載: gettimeofday.ccode
- #include <stdio.h>
- #include <sys/time.h>
-
- 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;
- }
gettimeofday能獲得微秒數,比毫秒還要更精確。
使用ftime接口:
下載: ftime.c
- #include <stdio.h>
- #include <sys/timeb.h>
-
- long long getSystemTime() {
- struct timeb t;
- ftime(&t);
- return 1000 * t.time + t.millitm;
- }
-
- int main() {
- long long start=getSystemTime();
- sleep(3);
- long long end=getSystemTime();
-
- printf("time: %lld ms\n", end-start);
- return 0;
- }
Windows系統
使用GetTickCount接口:
下載: GetTickCount.c
- #include <windows.h>
- #include <stdio.h>
-
- int main() {
- DWORD start, stop;
- start = GetTickCount();
- Sleep(3000);
- stop = GetTickCount();
- printf("time: %lld ms\n", stop - start);
- return 0;
- }
Windows系統下有些編譯器使用printf輸出64位整數參數要使用%I64d,好比VC。
使用QueryPerformanceX接口:
下載: QueryPerformance.c
- #include <windows.h>
- #include <stdio.h>
-
- int main(){
- LARGE_INTEGER li;
- LONGLONG start, end, freq;
- QueryPerformanceFrequency(&li);
- freq = li.QuadPart;
- QueryPerformanceCounter(&li);
- start = li.QuadPart;
- Sleep(3000);
- QueryPerformanceCounter(&li);
- end = li.QuadPart;
- int useTime =(int)((end - start) * 1000 / freq);
- printf("time: %d ms\n", useTime);
- return 0;
- }
使用GetSystemTime接口:
下載: GetSystemTime.c
- #include <windows.h>
- #include <stdio.h>
-
- int main(){
- SYSTEMTIME currentTime;
- GetSystemTime(¤tTime);
- printf("time: %u/%u/%u %u:%u:%u:%u %d\n",
- currentTime.wYear,currentTime.wMonth,currentTime.wDay,
- currentTime.wHour,currentTime.wMinute,currentTime.wSecond,
- currentTime.wMilliseconds,currentTime.wDayOfWeek);
- return 0;
- }
這種方法沒給出計算時間差的實現,只給出如何用GetSystemTime調用獲得當前時間,計算時間差比較簡單,根據年、月、日、時、分秒和毫秒計算出一個整數,再將兩整數相減便可。
後記
以上是經過Google找到一些用C語言得到精確到毫秒的實現方法,對比Linux和Windows的方法,發現兩個系統的API命名很不同,Linux接口名要麼都是小寫要麼使用下劃線(_)來分隔單詞,而Windows接口名中的單詞首字母大寫。