【問題描述】程式運作時間是程式設計的一項重要名額,如何測算程式的運作時間呢?
【解析】
測試函數
[html] view plain copy
- #include <math.h>
- void function()
- {
- unsigned int i,j;
- double y;
- for(i=0;i<1000;i++)
- for(j=0;j<1000;j++)
- y=sin((double)i);
- }
方法1 如果在QT中,利用QTime,其精度為ms級----------在QT中使用。
[html] view plain copy
- #include <QDebug>
- #include <QTime>
- QTime time;
- time.start();
- function();
- qDebug()<<time.elapsed()/1000.0<<"s";
運作結果:0.109 s
方法2 利用gettimeofday(),其精度為us級----------C語言實作,在Linux下使用。
[html] view plain copy
- #include <QDebug>
- #include <sys/time.h>
- struct timeval tpstart,tpend;
- float timeuse;
- gettimeofday(&tpstart,NULL);
- function();
- gettimeofday(&tpend,NULL);
- timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;
- qDebug()<<timeuse<<"s";
運作結果:0.109375 s
方法3 利用clock(),其精度為ms級----------C++實作,在VC和QT中也可以使用。
[html] view plain copy
- #include <QDebug>
- #include <sys/time.h>
- double time_Start = (double)clock();
- function();
- double time_End = (double)clock();
- qDebug()<<(time_End - time_Start)/1000.0<<"s";
運作結果:0.11 s
方法4 利用windows.h(VC)函數,其精度為us級----------在VC和QT中使用。
[html] view plain copy
- #include <QDebug>
- #include <windows.h>
- LARGE_INTEGER litmp;
- LONGLONG Qpart1,Qpart2,Useingtime;
- double dfMinus,dfFreq,dfTime;
- //獲得CPU計時器的時鐘頻率
- QueryPerformanceFrequency(&litmp);//取得高精度運作計數器的頻率f,機關是每秒多少次(n/s),
- dfFreq = (double)litmp.QuadPart;
- QueryPerformanceCounter(&litmp);//取得高精度運作計數器的數值
- Qpart1 = litmp.QuadPart; //開始計時
- function(); //待測試的計算函數等
- QueryPerformanceCounter(&litmp);//取得高精度運作計數器的數值
- Qpart2 = litmp.QuadPart; //終止計時
- dfMinus = (double)(Qpart2 - Qpart1);//計算計數器值
- dfTime = dfMinus / dfFreq;//獲得對應時間,機關為秒,可以乘1000000精确到微秒級(us)
- Useingtime = dfTime*1000000;
- qDebug()<<dfTime<<"s";
運作結果:0.107415 s
【代碼清單】
[html] view plain copy
- #include <QDebug>
- #include <QTime>
- #include <sys/time.h>
- #include <windows.h>
- #include <math.h>
- void function();
- int main(void)
- {
- qDebug()<<"-------------------------------";
- //-1-
- QTime time;
- time.start();
- function();
- qDebug()<<time.elapsed()/1000.0<<"s";
- //-2-
- struct timeval tpstart,tpend;
- float timeuse;
- gettimeofday(&tpstart,NULL);
- function();
- gettimeofday(&tpend,NULL);
- timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;
- qDebug()<<timeuse<<"s";
- //-3-
- double time_Start = (double)clock();
- function();
- double time_End = (double)clock();
- qDebug()<<(time_End - time_Start)/1000.0<<"s";
- //-4-
- LARGE_INTEGER litmp;
- LONGLONG Qpart1,Qpart2,Useingtime;
- double dfMinus,dfFreq,dfTime;
- //獲得CPU計時器的時鐘頻率
- QueryPerformanceFrequency(&litmp);//取得高精度運作計數器的頻率f,機關是每秒多少次(n/s),
- dfFreq = (double)litmp.QuadPart;
- QueryPerformanceCounter(&litmp);//取得高精度運作計數器的數值
- Qpart1 = litmp.QuadPart; //開始計時
- function(); //待測試的計算函數等
- QueryPerformanceCounter(&litmp);//取得高精度運作計數器的數值
- Qpart2 = litmp.QuadPart; //終止計時
- dfMinus = (double)(Qpart2 - Qpart1);//計算計數器值
- dfTime = dfMinus / dfFreq;//獲得對應時間,機關為秒,可以乘1000000精确到微秒級(us)
- Useingtime = dfTime*1000000;
- qDebug()<<dfTime<<"s";
- return 0;
- }
- void function()
- {
- unsigned int i,j;
- double y;
- for(i=0;i<1000;i++)
- for(j=0;j<1000;j++)
- y=sin((double)i);
- }
- ----------------------------------------------------------------------------
- 另外,方法2可具體參考:
-
在測試程式時,我們往往需要了解程式執行所需的時間,在C語言可以使用函數gettimeofday來得到時間,它的調用格式是:
#include <sys/time.h>int gettimeofday(struct timeval *tv,struct timezone *tz);int settimeofday(const struct timeval *tv , const structtimezone *tz);
其中結構timeval的定義為:
strut timeval {long tv_sec;long tv_usec;};
可以看出,使用這種方式計時,精度可達微秒。
進行計時的時候,我們需要前後調用兩次gettimeofday,然後計算中間的內插補點,執行個體代碼如下:
************************************
gettimeofday( &start, NULL );
foo(); //示例函數
gettimeofday( &end, NULL);
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;
timeuse /=1000000;
***********************************
其中start,end 是結構體所定義的timeval型。
- --------------------------------------------------------------------------------
- 方法3可具體參考:
-
我現在用C++語言寫了一段程式,想計算這段程式運作的準确時間,這是要用于跟其它實驗結果作對比的,是以要精确到毫秒,C++程式運作時間 确實很難掌握啊!
C++程式運作時間中的計時函數是clock(),而與其相關的資料類型是clock_t。在MSDN中,查得對clock函數定義如下:
這個函數傳回從“開啟這個程式程序”到“程式中調用clock()函數”時之間的CPU時鐘計時單元(clock tick)數,在MSDN中稱之為挂鐘時間(wal-clock)。其中clock_t是用來儲存時間的資料類型,在time.h檔案中,我們可以找到對 它的定義:- #ifndef _CLOCK_T_DEFINED
- typedef long clock_t;
- #define _CLOCK_T_DEFINED
- #endif
很明顯,clock_t是一個長整形數。在time.h檔案中,還定義了一個常量CLOCKS_PER_SEC,它用來表示一秒鐘會有多少個時鐘計時單元,其定義如下:- #ifndef _CLOCK_T_DEFINED
- typedef long clock_t;
- #define _CLOCK_T_DEFINED
- #endif
可以看到每過千分之一秒(1毫秒),調用clock()函數傳回的值就加1。下面舉個例子,你可以使用公式clock()/CLOCKS_PER_SEC來計算一個程序自身的C++程式運作時間 :- int main( void )
- {
- long i = 10000000L;
- clock_t start, finish;
- double duration;
- /* 測量一個事件持續的時間*/
- printf( "Time to do %ld empty loops is ", i );
- start = clock();
- while( i-- ) ;
- finish = clock();
- duration = (double)(finish - start) / CLOCKS_PER_SEC;
- printf( "%f seconds"n", duration );
- system("pause");
- }
當然,你也可以用clock函數來計算你的機器運作一個循環或者處理其它事件到底花了多少時間:- void elapsed_time()
- {
- printf("Elapsed time:%u secs."n",clock()/CLOCKS_PER_SEC);
- }
上面我們看到時鐘計時單元的長度為1毫秒,那麼計時的精度也為1毫秒,那麼我們可不可以通過改變CLOCKS_PER_SEC的定義,通過把它定義的大一 些,進而使計時精度更高呢?通過嘗試,你會發現這樣是不行的。在标準C++程式運作時間 中,最小的計時機關是一毫秒。- int main( void )
- {
- long i = 10000000L;
- clock_t start, finish;
- double duration;
- /* 測量一個事件持續的時間*/
- printf( "Time to do %ld empty loops is ", i );
- start = clock();
- while( i-- ) ;
- finish = clock();
- duration = (double)(finish - start) / CLOCKS_PER_SEC;
- printf( "%f seconds"n", duration );
- system("pause");
- }