日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C/C++中计算程序运行时间

發布時間:2024/9/30 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C/C++中计算程序运行时间 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


http://blog.csdn.net/trustbo/article/details/10582287


以前經常聽人提起如何計算程序運行時間,給出一系列函數,當時沒有注意,隨便選了clock()最簡單的方式進行計算。等到真正需要檢測程序性能提升了多少,才發現這里面有很多要注意的地方。


最常用的的方式: #include time_t start = clock(); time_t end = clock(); printf("the running time is : %f\n", double(end -begin)/CLOCKS_PER_SEC); clock()計算的是CPU執行耗時,注意是CPU!如果有多個核并行,最后的結果是每個CPU上運算時間的總和!想要精確到毫秒,可以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接口的參數的單位是毫秒,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系統下的多種實現方法,希望對大家有幫助。

    Linux系統

    使用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能得到微秒數,比毫秒還要更精確。

    使用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系統

    使用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系統下有些編譯器使用printf輸出64位整數參數要使用%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(&currentTime);
  • ????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調用得到當前時間,計算時間差比較簡單,根據年、月、日、時、分秒和毫秒計算出一個整數,再將兩整數相減即可。


    結論:

    最靠譜的還是用gettimeofday

    總結

    以上是生活随笔為你收集整理的C/C++中计算程序运行时间的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。