程序內(nèi)如何計(jì)算一個(gè)函數(shù)的執(zhí)行時(shí)間?
編譯
gcc runtime.c -lrt
注意需要增加動(dòng)態(tài)鏈接庫lrt,函數(shù)clock_gettime()定義于該庫中。
執(zhí)行結(jié)果如下:
root@ubuntu:/home/peng/zhh# ./a.out
you can call your function here
CLOCK_M(jìn)ONOTONIC reports 0.000013689 seconds
3. 實(shí)例2-更完善的一個(gè)例子
第一個(gè)實(shí)例比較簡單,實(shí)際在應(yīng)用中,尤其是在網(wǎng)絡(luò)通信中,經(jīng)常需要計(jì)算收發(fā)數(shù)據(jù)包的總共時(shí)間,以網(wǎng)絡(luò)的速率,F(xiàn)在我們增加功能如下:
檢查執(zhí)行函數(shù)前后的時(shí)間戳合法性,因?yàn)橛袝r(shí)候記錄的時(shí)間會(huì)比較長,會(huì)有數(shù)據(jù)溢出等問題循環(huán)累加總共執(zhí)行時(shí)間,計(jì)算總共執(zhí)行時(shí)間,然后根據(jù)執(zhí)行次數(shù)計(jì)算平均執(zhí)行時(shí)間a) 檢查時(shí)間合法性timespec_check()static int timespec_check(struct timespec *t)
{
if((t->tv_nsec <0 ) || (t->tv_nsec >= 1000000000))
return -1;
return 0;
}
功能:
該函數(shù)檢查時(shí)間戳的成員tv_nsec,該值不能小于0,也不能大于1000000000
參數(shù):
t 時(shí)間戳
返回值
成功返回 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.",
(long long) t1->tv_sec,t1->tv_nsec);
return;
}
if (timespec_check(t2) < 0) {
fprintf(stderr, "invalid time #2: %lld.%.9ld.",
(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;
}
}
功能:
該函數(shù)首先檢查參數(shù)t1、t2合法性,然后用t1的時(shí)間減去t2的時(shí)間,并把結(jié)果存放到t1
參數(shù):
t1:對應(yīng)函數(shù)執(zhí)行執(zhí)行結(jié)束的時(shí)間
t2:對應(yīng)函數(shù)執(zhí)行之前的時(shí)間
返回值:
無
b) 實(shí)現(xiàn) 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.",
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.",
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_M(jìn)ONOTONIC, &ts_start);
59 usleep(200);
60
61 rc = clock_gettime(CLOCK_M(jìn)ONOTONIC, &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_M(jìn)ONOTONIC reports %ld.%09ld seconds",
69 ts_end.tv_sec, ts_end.tv_nsec);
70 #endif
71 }
72 printf("** Total time %lds + %ld nsec",t_time_s,t_time_n);
73 }
編譯執(zhí)行如下:
root@ubuntu:/home/peng/zhh# ./a.out
** Total time 0s + 9636103 nsec
三、計(jì)算程序的執(zhí)行時(shí)間
有時(shí)候我們還想知道執(zhí)行某個(gè)程序需要多少時(shí)間,我們可以借助命令time。
1. 命令time
Linux time命令的用途,在于量測特定指令執(zhí)行時(shí)所需消耗的時(shí)間及系統(tǒng)資源等信息。
CPU資源的統(tǒng)計(jì)包括實(shí)際使用時(shí)間(real time)、用戶態(tài)使用時(shí)間(the process spent in user mode)、內(nèi)核態(tài)使用時(shí)間(the process spent in kernel mode)。
2. 語法time [options] COMMAND [arguments]
3. 例11. 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
在以上實(shí)例中,執(zhí)行命令"time date"(見第1行)。系統(tǒng)先執(zhí)行命令"date",第2行為命令"date"的執(zhí)行結(jié)果。第3-6行為執(zhí)行命令"date"的時(shí)間統(tǒng)計(jì)結(jié)果,其中第4行"real"為實(shí)際時(shí)間,第5行"user"為用戶CPU時(shí)間,第6行"sys"為系統(tǒng)CPU時(shí)間。以上三種時(shí)間的顯示格式均為MMmNN[.FFF]s。5. 例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)重新編譯執(zhí)行,10秒后會(huì)打印如下執(zhí)行結(jié)果:
root@ubuntu:/home/peng/zhh# time ./a.out
** Total time 10s + 8178015 nsec
real 0m10.009s
user 0m0.000s
sys 0m0.000s
結(jié)果和預(yù)期基本一致。
大家可以根據(jù)我的代碼,方便的將該功能移植到自己的項(xiàng)目中。

請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個(gè)字
最新活動(dòng)更多
-
6月20日立即下載>> 【白皮書】精準(zhǔn)測量 安全高效——福祿克光伏行業(yè)解決方案
-
7月3日立即報(bào)名>> 【在線會(huì)議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報(bào)名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會(huì)
-
7.30-8.1火熱報(bào)名中>> 全數(shù)會(huì)2025(第六屆)機(jī)器人及智能工廠展
-
7月31日免費(fèi)預(yù)約>> OFweek 2025具身機(jī)器人動(dòng)力電池技術(shù)應(yīng)用大會(huì)
-
免費(fèi)參會(huì)立即報(bào)名>> 7月30日- 8月1日 2025全數(shù)會(huì)工業(yè)芯片與傳感儀表展
推薦專題
- 1 AI 眼鏡讓百萬 APP「集體失業(yè)」?
- 2 大廠紛紛入局,百度、阿里、字節(jié)搶奪Agent話語權(quán)
- 3 深度報(bào)告|中國AI產(chǎn)業(yè)正在崛起成全球力量,市場潛力和關(guān)鍵挑戰(zhàn)有哪些?
- 4 上海跑出80億超級獨(dú)角獸:獲上市公司戰(zhàn)投,干人形機(jī)器人
- 5 一文看懂視覺語言動(dòng)作模型(VLA)及其應(yīng)用
- 6 國家數(shù)據(jù)局局長劉烈宏調(diào)研格創(chuàng)東智
- 7 下一代入口之戰(zhàn):大廠為何紛紛押注智能體?
- 8 百億AI芯片訂單,瘋狂傾銷中東?
- 9 Robotaxi新消息密集釋放,量產(chǎn)元年誰在領(lǐng)跑?
- 10 格斗大賽出圈!人形機(jī)器人致命短板曝光:頭腦過于簡單