如何计算函数的执行时间?

开发 后端
函数的执行时间的统计在嵌入式系统中会被频繁的用到,知识点很重要,本文从程序内计算一个函数的执行时间,计算一个程序的执行时间两个方面来讨论类似的问题。

[[383374]]

关于时间的文章,大家可以参考我之前的一篇文章《C语言操作时间函数,实现定时执行某个任务小程序》

0、问题描述

粉丝想计算一个函数的执行时间。


一、问题分析

函数的执行时间的统计在嵌入式系统中会被频繁的用到,知识点很重要。本文从两个方面来讨论类似的问题:

  1. 程序内计算一个函数的执行时间
  2. 计算一个程序的执行时间

二、程序内如何计算一个函数的执行时间?

1. 思路

我们在执行函数前后分别记录下时间戳,然后计算两个时间戳的差值即可。

我们需要借助函数clock_gettime来实现这个功能。看下该函数的定义:

  1. #include <time.h> 
  2.  
  3. int clock_gettime(clockid_t clk_id, struct timespec* tp); 
  4.  
  5. 可以根据需要,获取不同要求的精确时间 
  6.  
  7. 参数: 
  8. clk_id :  
  9.  检索和设置的clk_id指定的时钟时间。 
  10.  CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户改成其他,则对应的时间相应改变 
  11.   CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响 
  12.   CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间 
  13.   CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间 
  14. tp :  
  15.  获取的时间戳会存放到该结构体变量中 
  16.  struct timespec 
  17.  { 
  18.          time_t tv_sec; /* 秒*/ 
  19.          long tv_nsec; /* 纳秒*/ 
  20.  }; 
  21. 返回值: 
  22.  成功  0 
  23.  失败 -1  ,同时errno会被赋值 

因为我们希望计算执行某个函数的时间,所以我们第一个参数选择CLOCK_MONOTONIC。

2. 实例1

我们先来实现一个简单的程序:

  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行 我们可以将自己要执行的函数放置在此处。

编译

  1. gcc runtime.c -lrt 

注意需要增加动态链接库lrt,函数clock_gettime()定义于该库中。

执行结果如下:

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

3. 实例2-更完善的一个例子

第一个实例比较简单,实际在应用中,尤其是在网络通信中,经常需要计算收发数据包的总共时间,以网络的速率。现在我们增加功能如下:

  1. 检查执行函数前后的时间戳合法性,因为有时候记录的时间会比较长,会有数据溢出等问题
  2. 循环累加总共执行时间,计算总共执行时间,然后根据执行次数计算平均执行时间

a) 检查时间合法性

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.  该函数检查时间戳的成员tv_nsec,该值不能小于0,也不能大于1000000000 
  8. 参数: 
  9.  t 时间戳 
  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 超过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.  该函数首先检查参数t1、t2合法性,然后用t1的时间减去t2的时间,并把结果存放到t1 
  27. 参数: 
  28.  t1:对应函数执行执行结束的时间 
  29.  t2:对应函数执行之前的时间 
  30. 返回值: 
  31.  无 

b) 实现

  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 } 

编译执行如下:

  1. root@ubuntu:/home/peng/zhh# ./a.out  
  2. ** 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. 语法

  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 
  • 在以上实例中,执行命令"time date"(见第1行)。
  • 系统先执行命令"date",第2行为命令"date"的执行结果。
  • 第3-6行为执行命令"date"的时间统计结果,其中第4行"real"为实际时间,第5行"user"为用户CPU时间,第6行"sys"为系统CPU时间。以上三种时间的显示格式均为MMmNN[.FFF]s。

4. 例2

我们也可以测试上一章我们编写的程序:

  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) 重新编译执行,10秒后会打印如下执行结果:

  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 

结果和预期基本一致。

大家可以根据我的代码,方便的将该功能移植到自己的项目中。

 

责任编辑:姜华 来源: 一口Linux
相关推荐

2010-04-28 12:33:36

Oracle自定义函数

2011-05-17 13:32:04

oracle

2018-07-18 15:13:56

MCU代码时间

2010-09-06 13:17:19

SQL Server语句

2009-11-26 11:05:44

PHP计算页面执行时间

2024-04-12 07:50:40

Python监控利器Time 模块

2010-09-08 15:00:03

SQL语句执行

2010-11-18 15:53:30

Oracle语句执行时

2022-09-13 08:51:26

Python性能优化

2019-08-28 07:45:45

数据存储层多线程

2020-07-14 08:17:26

代码执行时间

2020-08-03 16:00:31

Linux命令进程

2018-11-22 09:15:45

Linux命令进程

2021-11-05 07:47:55

API计算任务

2023-01-27 15:28:04

开发Python内存

2020-12-25 08:52:53

SQLMysql 数据库

2012-01-10 10:44:36

字符串

2020-12-04 11:00:18

MySQL执行时间执行计划

2021-08-18 08:32:09

代码运行时间示波器

2022-12-12 08:17:29

点赞
收藏

51CTO技术栈公众号