偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何計(jì)算函數(shù)的執(zhí)行時(shí)間?

開(kāi)發(fā) 后端
函數(shù)的執(zhí)行時(shí)間的統(tǒng)計(jì)在嵌入式系統(tǒng)中會(huì)被頻繁的用到,知識(shí)點(diǎn)很重要,本文從程序內(nèi)計(jì)算一個(gè)函數(shù)的執(zhí)行時(shí)間,計(jì)算一個(gè)程序的執(zhí)行時(shí)間兩個(gè)方面來(lái)討論類(lèi)似的問(wèn)題。

[[383374]]

關(guān)于時(shí)間的文章,大家可以參考我之前的一篇文章《C語(yǔ)言操作時(shí)間函數(shù),實(shí)現(xiàn)定時(shí)執(zhí)行某個(gè)任務(wù)小程序》

0、問(wèn)題描述

粉絲想計(jì)算一個(gè)函數(shù)的執(zhí)行時(shí)間。


一、問(wèn)題分析

函數(shù)的執(zhí)行時(shí)間的統(tǒng)計(jì)在嵌入式系統(tǒng)中會(huì)被頻繁的用到,知識(shí)點(diǎn)很重要。本文從兩個(gè)方面來(lái)討論類(lèi)似的問(wèn)題:

  1. 程序內(nèi)計(jì)算一個(gè)函數(shù)的執(zhí)行時(shí)間
  2. 計(jì)算一個(gè)程序的執(zhí)行時(shí)間

二、程序內(nèi)如何計(jì)算一個(gè)函數(shù)的執(zhí)行時(shí)間?

1. 思路

我們?cè)趫?zhí)行函數(shù)前后分別記錄下時(shí)間戳,然后計(jì)算兩個(gè)時(shí)間戳的差值即可。

我們需要借助函數(shù)clock_gettime來(lái)實(shí)現(xiàn)這個(gè)功能??聪略摵瘮?shù)的定義:

  1. #include <time.h> 
  2.  
  3. int clock_gettime(clockid_t clk_id, struct timespec* tp); 
  4.  
  5. 可以根據(jù)需要,獲取不同要求的精確時(shí)間 
  6.  
  7. 參數(shù): 
  8. clk_id :  
  9.  檢索和設(shè)置的clk_id指定的時(shí)鐘時(shí)間。 
  10.  CLOCK_REALTIME:系統(tǒng)實(shí)時(shí)時(shí)間,隨系統(tǒng)實(shí)時(shí)時(shí)間改變而改變,即從UTC1970-1-1 0:0:0開(kāi)始計(jì)時(shí),中間時(shí)刻如果系統(tǒng)時(shí)間被用戶(hù)改成其他,則對(duì)應(yīng)的時(shí)間相應(yīng)改變 
  11.   CLOCK_MONOTONIC:從系統(tǒng)啟動(dòng)這一刻起開(kāi)始計(jì)時(shí),不受系統(tǒng)時(shí)間被用戶(hù)改變的影響 
  12.   CLOCK_PROCESS_CPUTIME_ID:本進(jìn)程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間 
  13.   CLOCK_THREAD_CPUTIME_ID:本線程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間 
  14. tp :  
  15.  獲取的時(shí)間戳?xí)娣诺皆摻Y(jié)構(gòu)體變量中 
  16.  struct timespec 
  17.  { 
  18.          time_t tv_sec; /* 秒*/ 
  19.          long tv_nsec; /* 納秒*/ 
  20.  }; 
  21. 返回值: 
  22.  成功  0 
  23.  失敗 -1  ,同時(shí)errno會(huì)被賦值 

因?yàn)槲覀兿M?jì)算執(zhí)行某個(gè)函數(shù)的時(shí)間,所以我們第一個(gè)參數(shù)選擇CLOCK_MONOTONIC。

2. 實(shí)例1

我們先來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的程序:

  1.  1 #include <stdio.h>                                                                
  2.  2 #include <stdlib.h> 
  3.  3 #include <stdint.h> 
  4.  4 #include <time.h> 
  5.  5 #include <sys/time.h> 
  6.  6 #include <sys/stat.h> 
  7.  7 #include <sys/types.h> 
  8.  8 #include <unistd.h> 
  9.  9 #include <string.h> 
  10. 10  
  11. 11 int main() 
  12. 12 { 
  13. 13     int rc; 
  14. 14     struct timespec ts_start, ts_end; 
  15. 15      
  16. 16     //start time before call function 
  17. 17     rc = clock_gettime(CLOCK_MONOTONIC, &ts_start); 
  18. 18      
  19. 19     printf("you can call your function here\n"); 
  20. 20      
  21. 21     //end time before call function  
  22. 22     rc = clock_gettime(CLOCK_MONOTONIC, &ts_end); 
  23. 23      
  24. 24     printf("CLOCK_MONOTONIC reports %ld.%09ld seconds\n"
  25. 25             ts_end.tv_sec - ts_start.tv_sec, ts_end.tv_nsec - ts_start.tv_nsec); 
  26. 26 } 

19行 我們可以將自己要執(zhí)行的函數(shù)放置在此處。

編譯

  1. gcc runtime.c -lrt 

注意需要增加動(dòng)態(tài)鏈接庫(kù)lrt,函數(shù)clock_gettime()定義于該庫(kù)中。

執(zhí)行結(jié)果如下:

  1. root@ubuntu:/home/peng/zhh# ./a.out  
  2. you can call your function here 
  3. CLOCK_MONOTONIC reports 0.000013689 seconds 

3. 實(shí)例2-更完善的一個(gè)例子

第一個(gè)實(shí)例比較簡(jiǎn)單,實(shí)際在應(yīng)用中,尤其是在網(wǎng)絡(luò)通信中,經(jīng)常需要計(jì)算收發(fā)數(shù)據(jù)包的總共時(shí)間,以網(wǎng)絡(luò)的速率?,F(xiàn)在我們?cè)黾庸δ苋缦拢?/p>

  1. 檢查執(zhí)行函數(shù)前后的時(shí)間戳合法性,因?yàn)橛袝r(shí)候記錄的時(shí)間會(huì)比較長(zhǎng),會(huì)有數(shù)據(jù)溢出等問(wèn)題
  2. 循環(huán)累加總共執(zhí)行時(shí)間,計(jì)算總共執(zhí)行時(shí)間,然后根據(jù)執(zhí)行次數(shù)計(jì)算平均執(zhí)行時(shí)間

a) 檢查時(shí)間合法性

timespec_check()

  1. static int timespec_check(struct timespec *t) 
  2.  if((t->tv_nsec <0 ) || (t->tv_nsec >= 1000000000)) 
  3.   return -1; 
  4.  
  5.  return 0; 
  6. 功能: 
  7.  該函數(shù)檢查時(shí)間戳的成員tv_nsec,該值不能小于0,也不能大于1000000000 
  8. 參數(shù): 
  9.  t 時(shí)間戳 
  10. 返回值 
  11.  成功返回 0 
  12.  非法返回-1 

timespec_sub()

  1. static void timespec_sub(struct timespec *t1,  struct timespec *t2) 
  2.  if (timespec_check(t1) < 0) { 
  3.   fprintf(stderr, "invalid time #1: %lld.%.9ld.\n"
  4.    (long long) t1->tv_sec,t1->tv_nsec); 
  5.   return
  6.  } 
  7.  if (timespec_check(t2) < 0) { 
  8.   fprintf(stderr, "invalid time #2: %lld.%.9ld.\n"
  9.    (long long) t2->tv_sec,t2->tv_nsec); 
  10.   return
  11.  }  
  12.  
  13.  t1->tv_sec -= t2->tv_sec; 
  14.  t1->tv_nsec -= t2->tv_nsec; 
  15.  if (t1->tv_nsec >= 1000000000) 
  16.  {//tv_nsec 超過(guò)1000000000,秒需要加1 
  17.   t1->tv_sec++; 
  18.   t1->tv_nsec -= 1000000000; 
  19.  } 
  20.  else if (t1->tv_nsec < 0) 
  21.  {//tv_nsec 小于0,秒需要減1 
  22.   t1->tv_sec--; 
  23.   t1->tv_nsec += 1000000000; 
  24.  } 
  25. 功能: 
  26.  該函數(shù)首先檢查參數(shù)t1、t2合法性,然后用t1的時(shí)間減去t2的時(shí)間,并把結(jié)果存放到t1 
  27. 參數(shù): 
  28.  t1:對(duì)應(yīng)函數(shù)執(zhí)行執(zhí)行結(jié)束的時(shí)間 
  29.  t2:對(duì)應(yīng)函數(shù)執(zhí)行之前的時(shí)間 
  30. 返回值: 
  31.  無(wú) 

b) 實(shí)現(xiàn)

  1.  1 #include <stdio.h>  
  2.  2 #include <stdlib.h> 
  3.  3 #include <stdint.h> 
  4.  4 #include <time.h> 
  5.  5 #include <sys/time.h> 
  6.  6 #include <sys/stat.h> 
  7.  7 #include <sys/types.h> 
  8.  8 #include <unistd.h> 
  9.  9 #include <string.h> 
  10. 10  
  11. 11  
  12. 12 static int timespec_check(struct timespec *t) 
  13. 13 { 
  14. 14     if((t->tv_nsec <0 ) || (t->tv_nsec >= 1000000000)) 
  15. 15         return -1; 
  16. 16  
  17. 17     return 0; 
  18. 18 } 
  19. 19  
  20. 20 static void timespec_sub(struct timespec *t1,  struct timespec *t2) 
  21. 21 { 
  22. 22     if (timespec_check(t1) < 0) { 
  23. 23         fprintf(stderr, "invalid time #1: %lld.%.9ld.\n"
  24. 24             (long long) t1->tv_sec,t1->tv_nsec); 
  25. 25         return
  26. 26     } 
  27. 27     if (timespec_check(t2) < 0) { 
  28. 28         fprintf(stderr, "invalid time #2: %lld.%.9ld.\n"
  29. 29             (long long) t2->tv_sec,t2->tv_nsec); 
  30. 30         return
  31. 31     } 
  32. 32  
  33. 33     t1->tv_sec -= t2->tv_sec; 
  34. 34     t1->tv_nsec -= t2->tv_nsec; 
  35. 35     if (t1->tv_nsec >= 1000000000) 
  36. 36     { 
  37. 37         t1->tv_sec++; 
  38. 38         t1->tv_nsec -= 1000000000; 
  39. 39     } 
  40. 40     else if (t1->tv_nsec < 0) 
  41. 41     { 
  42. 42         t1->tv_sec--; 
  43. 43         t1->tv_nsec += 1000000000; 
  44. 44     } 
  45. 45 } 
  46. 46  
  47. 47 int main() 
  48. 48 { 
  49. 49     int rc; 
  50. 50     int count = 10; 
  51. 51     long t_time_n = 0;  //nano secend 
  52. 52     long t_time_s = 0;  //secnd 
  53. 53     struct timespec ts_start, ts_end; 
  54. 54  
  55. 55  
  56. 56     while (count--) { 
  57. 57  
  58. 58         rc = clock_gettime(CLOCK_MONOTONIC, &ts_start); 
  59. 59         usleep(200); 
  60. 60  
  61. 61         rc = clock_gettime(CLOCK_MONOTONIC, &ts_end);                                                          
  62. 62  
  63. 63         timespec_sub(&ts_end, &ts_start); 
  64. 64         t_time_n += ts_end.tv_nsec; 
  65. 65         t_time_s += ts_end.tv_sec; 
  66. 66  
  67. 67         #if 0 
  68. 68         printf("CLOCK_MONOTONIC reports %ld.%09ld seconds\n",  
  69. 69                 ts_end.tv_sec, ts_end.tv_nsec);      
  70. 70         #endif 
  71. 71     } 
  72. 72     printf("** Total time %lds + %ld nsec\n",t_time_s,t_time_n); 
  73. 73 } 

編譯執(zhí)行如下:

  1. root@ubuntu:/home/peng/zhh# ./a.out  
  2. ** Total time 0s + 9636103 nsec 

三、計(jì)算程序的執(zhí)行時(shí)間

有時(shí)候我們還想知道執(zhí)行某個(gè)程序需要多少時(shí)間,我們可以借助命令time。

1. 命令time

Linux time命令的用途,在于量測(cè)特定指令執(zhí)行時(shí)所需消耗的時(shí)間及系統(tǒng)資源等信息。

CPU資源的統(tǒng)計(jì)包括實(shí)際使用時(shí)間(real time)、用戶(hù)態(tài)使用時(shí)間(the process spent in user mode)、內(nèi)核態(tài)使用時(shí)間(the process spent in kernel mode)。

2. 語(yǔ)法

  1. time [options] COMMAND [arguments] 

3. 例1

  1. 1. root@ubuntu:/home/peng/zhh# time date   
  2. 2. Tue Feb 23 03:44:27 PST 2021 
  3. 3.  
  4. 4. real    0m0.001s 
  5. 5. user    0m0.000s 
  6. 6. sys     0m0.000s 
  • 在以上實(shí)例中,執(zhí)行命令"time date"(見(jiàn)第1行)。
  • 系統(tǒng)先執(zhí)行命令"date",第2行為命令"date"的執(zhí)行結(jié)果。
  • 第3-6行為執(zhí)行命令"date"的時(shí)間統(tǒng)計(jì)結(jié)果,其中第4行"real"為實(shí)際時(shí)間,第5行"user"為用戶(hù)CPU時(shí)間,第6行"sys"為系統(tǒng)CPU時(shí)間。以上三種時(shí)間的顯示格式均為MMmNN[.FFF]s。

4. 例2

我們也可以測(cè)試上一章我們編寫(xiě)的程序:

  1. root@ubuntu:/home/peng/zhh# time ./a.out  
  2. ** Total time 0s + 9649603 nsec, avg_time = -9649603.000000  
  3.  
  4. real 0m0.010s 
  5. user 0m0.000s 
  6. sys     0m0.000s 

下面我們將59行代碼中的usleep(200)修改成sleep(1) 重新編譯執(zhí)行,10秒后會(huì)打印如下執(zhí)行結(jié)果:

  1. root@ubuntu:/home/peng/zhh# time ./a.out  
  2. ** Total time 10s + 8178015 nsec 
  3.  
  4. real 0m10.009s 
  5. user 0m0.000s 
  6. sys  0m0.000s 

結(jié)果和預(yù)期基本一致。

大家可以根據(jù)我的代碼,方便的將該功能移植到自己的項(xiàng)目中。

 

責(zé)任編輯:姜華 來(lái)源: 一口Linux
相關(guān)推薦

2010-04-28 12:33:36

Oracle自定義函數(shù)

2011-05-17 13:32:04

oracle

2018-07-18 15:13:56

MCU代碼時(shí)間

2009-11-26 11:05:44

PHP計(jì)算頁(yè)面執(zhí)行時(shí)間

2024-04-12 07:50:40

Python監(jiān)控利器Time 模塊

2010-09-06 13:17:19

SQL Server語(yǔ)句

2010-09-08 15:00:03

SQL語(yǔ)句執(zhí)行

2025-01-16 07:00:00

AOPSpringBoot后端

2010-11-18 15:53:30

Oracle語(yǔ)句執(zhí)行時(shí)

2022-09-13 08:51:26

Python性能優(yōu)化

2020-07-14 08:17:26

代碼執(zhí)行時(shí)間

2019-08-28 07:45:45

數(shù)據(jù)存儲(chǔ)層多線程

2020-08-03 16:00:31

Linux命令進(jìn)程

2018-11-22 09:15:45

Linux命令進(jìn)程

2021-11-05 07:47:55

API計(jì)算任務(wù)

2024-05-10 08:44:53

C#軟件開(kāi)發(fā)優(yōu)化代碼

2024-07-03 13:51:02

SQL毛刺數(shù)據(jù)庫(kù)

2023-01-27 15:28:04

開(kāi)發(fā)Python內(nèi)存

2020-12-25 08:52:53

SQLMysql 數(shù)據(jù)庫(kù)

2012-01-10 10:44:36

字符串
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)