linux 統計 程式 執行時間

2022-01-23 11:58:23 字數 1319 閱讀 2829

測試 **執行時間

linux 中的 中 有個函式可以獲取當前時間,精確到 微秒 ---->  gettimeofday()

1 #include 

//int gettimeofday(struct timeval *tv, struct timezone *tz);2/*

********************************************

3* struct timeval4*

9**********************************************

10* struct timezone

11*

15*********************************************

*/

使用時,定義兩個 struct timeval  變數(通常 gettimeofday() 的第二個引數 設為 null),分別儲存 **測試 前後的時刻,最後相減,即可獲取 **執行時間 (可轉換為自己需要的時間)。

1 #include 2 #include //

for gettimeofday()

3 #include //

for memset()45

intmain()619

20//

-------------------------

21gettimeofday(&end, null); //

get the end time

22//

-------------------------

2324

long

long total_time = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); //

get the run time by microsecond

25 printf("

total time is %lld us\n

", total_time);

26 total_time /= 1000; //

get the run time by millisecond

27 printf("

total time is %lld ms\n

", total_time);

28 }

測試結果:(centos 6.5, gcc 4.4.7)

total time is 49658 us

total time is 49 ms

c 統計程式執行時間的方法

總結下c 中統計程式執行時間的方法。1 void main time t t2 time t2 printf 已執行 d秒 n t2 t1 2 vc的話有profile,在鏈結屬性頁勾選profile項,然後profile 在編譯選單下 各個函式時間都出來了3 int main 當然,你也可以用cl...

Python中統計程式執行時間的方法彙總

以下列舉常用統計程式執行時間的方法。import datetime starttime datetime.datetime.now long running do something other endtime datetime.datetime.now print endtime starttim...

Python中統計程式執行時間的方法彙總

以下列舉常用統計程式執行時間的方法。方法1 import datetime starttime datetime.datetime.now long running do something other endtime datetime.datetime.now print endtime star...