如何計算函數的執行時間?
關于時間的文章,大家可以參考我之前的一篇文章《C語言操作時間函數,實現定時執行某個任務小程序》
0、問題描述
粉絲想計算一個函數的執行時間。

一、問題分析
函數的執行時間的統計在嵌入式系統中會被頻繁的用到,知識點很重要。本文從兩個方面來討論類似的問題:
- 程序內計算一個函數的執行時間
- 計算一個程序的執行時間
二、程序內如何計算一個函數的執行時間?
1. 思路
我們在執行函數前后分別記錄下時間戳,然后計算兩個時間戳的差值即可。
我們需要借助函數clock_gettime來實現這個功能。看下該函數的定義:
- #include <time.h>
- int clock_gettime(clockid_t clk_id, struct timespec* tp);
- 可以根據需要,獲取不同要求的精確時間
- 參數:
- clk_id :
- 檢索和設置的clk_id指定的時鐘時間。
- CLOCK_REALTIME:系統實時時間,隨系統實時時間改變而改變,即從UTC1970-1-1 0:0:0開始計時,中間時刻如果系統時間被用戶改成其他,則對應的時間相應改變
- CLOCK_MONOTONIC:從系統啟動這一刻起開始計時,不受系統時間被用戶改變的影響
- CLOCK_PROCESS_CPUTIME_ID:本進程到當前代碼系統CPU花費的時間
- CLOCK_THREAD_CPUTIME_ID:本線程到當前代碼系統CPU花費的時間
- tp :
- 獲取的時間戳會存放到該結構體變量中
- struct timespec
- {
- time_t tv_sec; /* 秒*/
- long tv_nsec; /* 納秒*/
- };
- 返回值:
- 成功 0
- 失敗 -1 ,同時errno會被賦值
因為我們希望計算執行某個函數的時間,所以我們第一個參數選擇CLOCK_MONOTONIC。
2. 實例1
我們先來實現一個簡單的程序:
- 1 #include <stdio.h>
- 2 #include <stdlib.h>
- 3 #include <stdint.h>
- 4 #include <time.h>
- 5 #include <sys/time.h>
- 6 #include <sys/stat.h>
- 7 #include <sys/types.h>
- 8 #include <unistd.h>
- 9 #include <string.h>
- 10
- 11 int main()
- 12 {
- 13 int rc;
- 14 struct timespec ts_start, ts_end;
- 15
- 16 //start time before call function
- 17 rc = clock_gettime(CLOCK_MONOTONIC, &ts_start);
- 18
- 19 printf("you can call your function here\n");
- 20
- 21 //end time before call function
- 22 rc = clock_gettime(CLOCK_MONOTONIC, &ts_end);
- 23
- 24 printf("CLOCK_MONOTONIC reports %ld.%09ld seconds\n",
- 25 ts_end.tv_sec - ts_start.tv_sec, ts_end.tv_nsec - ts_start.tv_nsec);
- 26 }
19行 我們可以將自己要執行的函數放置在此處。
編譯
- gcc runtime.c -lrt
注意需要增加動態鏈接庫lrt,函數clock_gettime()定義于該庫中。
執行結果如下:
- root@ubuntu:/home/peng/zhh# ./a.out
- you can call your function here
- CLOCK_MONOTONIC reports 0.000013689 seconds
3. 實例2-更完善的一個例子
第一個實例比較簡單,實際在應用中,尤其是在網絡通信中,經常需要計算收發數據包的總共時間,以網絡的速率。現在我們增加功能如下:
- 檢查執行函數前后的時間戳合法性,因為有時候記錄的時間會比較長,會有數據溢出等問題
- 循環累加總共執行時間,計算總共執行時間,然后根據執行次數計算平均執行時間
a) 檢查時間合法性
timespec_check()
- static int timespec_check(struct timespec *t)
- {
- if((t->tv_nsec <0 ) || (t->tv_nsec >= 1000000000))
- return -1;
- return 0;
- }
- 功能:
- 該函數檢查時間戳的成員tv_nsec,該值不能小于0,也不能大于1000000000
- 參數:
- t 時間戳
- 返回值
- 成功返回 0
- 非法返回-1
timespec_sub()
- static void timespec_sub(struct timespec *t1, struct timespec *t2)
- {
- if (timespec_check(t1) < 0) {
- fprintf(stderr, "invalid time #1: %lld.%.9ld.\n",
- (long long) t1->tv_sec,t1->tv_nsec);
- return;
- }
- if (timespec_check(t2) < 0) {
- fprintf(stderr, "invalid time #2: %lld.%.9ld.\n",
- (long long) t2->tv_sec,t2->tv_nsec);
- return;
- }
- t1->tv_sec -= t2->tv_sec;
- t1->tv_nsec -= t2->tv_nsec;
- if (t1->tv_nsec >= 1000000000)
- {//tv_nsec 超過1000000000,秒需要加1
- t1->tv_sec++;
- t1->tv_nsec -= 1000000000;
- }
- else if (t1->tv_nsec < 0)
- {//tv_nsec 小于0,秒需要減1
- t1->tv_sec--;
- t1->tv_nsec += 1000000000;
- }
- }
- 功能:
- 該函數首先檢查參數t1、t2合法性,然后用t1的時間減去t2的時間,并把結果存放到t1
- 參數:
- t1:對應函數執行執行結束的時間
- t2:對應函數執行之前的時間
- 返回值:
- 無
b) 實現
- 1 #include <stdio.h>
- 2 #include <stdlib.h>
- 3 #include <stdint.h>
- 4 #include <time.h>
- 5 #include <sys/time.h>
- 6 #include <sys/stat.h>
- 7 #include <sys/types.h>
- 8 #include <unistd.h>
- 9 #include <string.h>
- 10
- 11
- 12 static int timespec_check(struct timespec *t)
- 13 {
- 14 if((t->tv_nsec <0 ) || (t->tv_nsec >= 1000000000))
- 15 return -1;
- 16
- 17 return 0;
- 18 }
- 19
- 20 static void timespec_sub(struct timespec *t1, struct timespec *t2)
- 21 {
- 22 if (timespec_check(t1) < 0) {
- 23 fprintf(stderr, "invalid time #1: %lld.%.9ld.\n",
- 24 (long long) t1->tv_sec,t1->tv_nsec);
- 25 return;
- 26 }
- 27 if (timespec_check(t2) < 0) {
- 28 fprintf(stderr, "invalid time #2: %lld.%.9ld.\n",
- 29 (long long) t2->tv_sec,t2->tv_nsec);
- 30 return;
- 31 }
- 32
- 33 t1->tv_sec -= t2->tv_sec;
- 34 t1->tv_nsec -= t2->tv_nsec;
- 35 if (t1->tv_nsec >= 1000000000)
- 36 {
- 37 t1->tv_sec++;
- 38 t1->tv_nsec -= 1000000000;
- 39 }
- 40 else if (t1->tv_nsec < 0)
- 41 {
- 42 t1->tv_sec--;
- 43 t1->tv_nsec += 1000000000;
- 44 }
- 45 }
- 46
- 47 int main()
- 48 {
- 49 int rc;
- 50 int count = 10;
- 51 long t_time_n = 0; //nano secend
- 52 long t_time_s = 0; //secnd
- 53 struct timespec ts_start, ts_end;
- 54
- 55
- 56 while (count--) {
- 57
- 58 rc = clock_gettime(CLOCK_MONOTONIC, &ts_start);
- 59 usleep(200);
- 60
- 61 rc = clock_gettime(CLOCK_MONOTONIC, &ts_end);
- 62
- 63 timespec_sub(&ts_end, &ts_start);
- 64 t_time_n += ts_end.tv_nsec;
- 65 t_time_s += ts_end.tv_sec;
- 66
- 67 #if 0
- 68 printf("CLOCK_MONOTONIC reports %ld.%09ld seconds\n",
- 69 ts_end.tv_sec, ts_end.tv_nsec);
- 70 #endif
- 71 }
- 72 printf("** Total time %lds + %ld nsec\n",t_time_s,t_time_n);
- 73 }
編譯執行如下:
- root@ubuntu:/home/peng/zhh# ./a.out
- ** Total time 0s + 9636103 nsec
三、計算程序的執行時間
有時候我們還想知道執行某個程序需要多少時間,我們可以借助命令time。
1. 命令time
Linux time命令的用途,在于量測特定指令執行時所需消耗的時間及系統資源等信息。
CPU資源的統計包括實際使用時間(real time)、用戶態使用時間(the process spent in user mode)、內核態使用時間(the process spent in kernel mode)。
2. 語法
- time [options] COMMAND [arguments]
3. 例1
- 1. root@ubuntu:/home/peng/zhh# time date
- 2. Tue Feb 23 03:44:27 PST 2021
- 3.
- 4. real 0m0.001s
- 5. user 0m0.000s
- 6. sys 0m0.000s
- 在以上實例中,執行命令"time date"(見第1行)。
- 系統先執行命令"date",第2行為命令"date"的執行結果。
- 第3-6行為執行命令"date"的時間統計結果,其中第4行"real"為實際時間,第5行"user"為用戶CPU時間,第6行"sys"為系統CPU時間。以上三種時間的顯示格式均為MMmNN[.FFF]s。
4. 例2
我們也可以測試上一章我們編寫的程序:
- root@ubuntu:/home/peng/zhh# time ./a.out
- ** Total time 0s + 9649603 nsec, avg_time = -9649603.000000
- real 0m0.010s
- user 0m0.000s
- sys 0m0.000s
下面我們將59行代碼中的usleep(200)修改成sleep(1) 重新編譯執行,10秒后會打印如下執行結果:
- root@ubuntu:/home/peng/zhh# time ./a.out
- ** Total time 10s + 8178015 nsec
- real 0m10.009s
- user 0m0.000s
- sys 0m0.000s
結果和預期基本一致。
大家可以根據我的代碼,方便的將該功能移植到自己的項目中。