C語言中對程式執行計時

2021-06-10 02:43:13 字數 734 閱讀 1960

使用time()函式。它在標頭檔案time.h中

具體使用方法如下:

time_t a,b;//time_t是表示時間的結構體,你可以在time.h中找到它的原型。

a=time(null);//表示獲取當前的機器時間。

**段b=time(null);//表示獲取當前的機器時間。

a是**段執行前的時間,b是**段執行後的時間(單位是秒),那麼b-a當然是**段的執行時間了。輸出時,以長整型輸出時間。

可以使用time.h裡面的clock函式

#include

#include

#include

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");

return 0;

可參考 

C 語言程式計時

我們測試乙個函式的執行時間時,就需要用到下面這套模板。include include clock t start,stop double duration 記錄被測函式執行時間,以s為單位 intmain 在time.h中,clock t 為長整形 typedef long clock t clk ...

C語言編寫程式計時

在我們程式設計的時候,有時候需要我們去監測程式執行的時間問題,尤其是在做加速或者做優化的時候,乙個程式執行的時間也許就是我們判斷程式好壞的標準,所以我們有時候要進行計時。因為用到了,所以做個記錄方便下次檢視 include stdio.h include time.h clock t start t...

C C 語言中的計時函式

1.中函式clock 返回 型別clock t,精確度,毫秒級別 include include include void test int main void 2.最精確的計時 queryperformancecounter來查詢定時器的計數值,如果硬體裡有定時器,它就會啟動這個定時器,並且不斷獲...