Linux下測試程式的執行時間

2021-06-19 19:30:05 字數 919 閱讀 2170

方法一:

要包含標頭檔案

#include "time.h"

#include

**如下:

[cpp]view plain

copy

/*...............測試程式執行時間...................*/

time_t

startt,endt;  

double

totalt;  

startt = time(null);  

// 假設一條語句執行10000次

//  obj.mysqlinsert("insert into children values(22,'zhangsan',50)");       //插入一行資料

for(

inti=0; i<10000; ++i)  

//這裡插入了1萬行資料

endt = time(null);  

totalt = difftime(endt,startt);  

cout << "程式執行的時間為:"

<< totalt << endl;  

方法二:

#include

#include

#include

int main()

gettimeofday(&tpend,null);

timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;

timeuse/=1000000;

printf("used time:%f\n",timeuse);

return 0;

} 方法三:

在執行程式前,加time,如:輸入time./abc

linux下計算程式執行時間

這裡介紹一下我在專案測試中用到的兩種方法 1 clock 1 include stdio.h 2 include stdlib.h 3 include time.h 45 intmain 6 這個函式返回開啟程序和呼叫clock 之間的的cpu時鐘計時單元 clock tick 數,在msdn中稱之...

測試程式執行時間 time h

1 計時 c c 中的計時函式是clock 而與其相關的資料型別是clock t。在msdn中,查得對clock函式定義如下 clock t clock void 這個函式返回從 開啟這個程式程序 到 程式中呼叫clock 函式 時之間的cpu時鐘計時單元 clock tick 數,在msdn中稱之...

C 測試程式執行時間

我們應當忘記小的效能優化,百分之九十七的情況下,過早的優化都是萬惡之源 這句話在很多時候都被引用到,以至於 不要優化 注意,是 不要優化 而不是 不要過早優化 已經深入人心,過度地推崇這條建議經常會成為如下行為的藉口 還有另乙個常識 優化是不重要的,這條常識的理由,在程式設計師工具箱中最強大的優化技...