生活随笔
收集整理的這篇文章主要介紹了
C/C++中计算程序运行时间
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://blog.csdn.net/trustbo/article/details/10582287
以前經(jīng)常聽人提起如何計(jì)算程序運(yùn)行時(shí)間,給出一系列函數(shù),當(dāng)時(shí)沒有注意,隨便選了clock()最簡單的方式進(jìn)行計(jì)算。等到真正需要檢測(cè)程序性能提升了多少,才發(fā)現(xiàn)這里面有很多要注意的地方。
最常用的的方式:
#include
time_t start = clock();
time_t end = clock();
printf("the running time is : %f\n", double(end -begin)/CLOCKS_PER_SEC);
clock()計(jì)算的是CPU執(zhí)行耗時(shí),注意是CPU!如果有多個(gè)核并行,最后的結(jié)果是每個(gè)CPU上運(yùn)算時(shí)間的總和!想要精確到毫秒,可以double(end -begin)*1000/CLOCKS_PER_SEC
一般來說,只要求精確到秒的話,time是很好使的
#include?<</span>stdio.h> #include?<</span>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接口的參數(shù)的單位是毫秒,Linux的sleep接口的參數(shù)的單位是秒。
如果需要精確到毫秒,以上程序就發(fā)揮不了作用,如果在Java要達(dá)到這要求就很簡單了,代碼如下所示:
下載:?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找了一些資料后,發(fā)現(xiàn)C語言里沒有標(biāo)準(zhǔn)的接口可以獲得精確到毫秒的時(shí)間,都會(huì)調(diào)用到與操作系統(tǒng)相關(guān)的API,下面會(huì)分別介紹在Linux和Windows系統(tǒng)下的多種實(shí)現(xiàn)方法,希望對(duì)大家有幫助。
Linux系統(tǒng)
使用gettimeofday接口:
下載:?gettimeofday.c
#include?<</span>stdio.h> #include?<</span>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能得到微秒數(shù),比毫秒還要更精確。
使用ftime接口:
下載:?ftime.c
#include?<</span>stdio.h> #include?<</span>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系統(tǒng)
使用GetTickCount接口:
下載:?GetTickCount.c
#include?<</span>windows.h> #include?<</span>stdio.h> ? int?main()?{ ????DWORD?start,?stop; ????start?=?GetTickCount(); ????Sleep(3000); ????stop?=?GetTickCount(); ????printf("time: %lld ms\n",?stop?-?start); ????return?0; } Windows系統(tǒng)下有些編譯器使用printf輸出64位整數(shù)參數(shù)要使用%I64d,比如VC。
使用QueryPerformanceX接口:
下載:?QueryPerformance.c
#include?<</span>windows.h> #include?<</span>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?<</span>windows.h> #include?<</span>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; } 這種方法沒給出計(jì)算時(shí)間差的實(shí)現(xiàn),只給出如何用GetSystemTime調(diào)用得到當(dāng)前時(shí)間,計(jì)算時(shí)間差比較簡單,根據(jù)年、月、日、時(shí)、分秒和毫秒計(jì)算出一個(gè)整數(shù),再將兩整數(shù)相減即可。
結(jié)論:
最靠譜的還是用gettimeofday
總結(jié)
以上是生活随笔為你收集整理的C/C++中计算程序运行时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。