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

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

[[383374]]

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

0、问题描述

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


一、问题分析

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

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

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

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会被赋值 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

因为我们希望计算执行某个函数的时间,所以我们第一个参数选择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 } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

19行 我们可以将自己要执行的函数放置在此处。

编译

gcc runtime.c -lrt 
  • 1.

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

执行结果如下:

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

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

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

  1. 检查执行函数前后的时间戳合法性,因为有时候记录的时间会比较长,会有数据溢出等问题
  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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

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:对应函数执行之前的时间 
返回值: 
 无 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

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 } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.

编译执行如下:

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

三、计算程序的执行时间

有时候我们还想知道执行某个程序需要多少时间,我们可以借助命令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] 
  • 1.

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 在以上实例中,执行命令"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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

下面我们将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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

结果和预期基本一致。

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

 

责任编辑:姜华 来源: 一口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语句

2024-04-12 07:50:40

Python监控利器Time 模块

2009-11-26 11:05:44

PHP计算页面执行时间

2010-09-08 15:00:03

SQL语句执行

2025-01-16 07:00:00

AOPSpringBoot后端

2010-11-18 15:53:30

Oracle语句执行时

2022-09-13 08:51:26

Python性能优化

2020-07-14 08:17:26

代码执行时间

2019-08-28 07:45:45

数据存储层多线程

2020-08-03 16:00:31

Linux命令进程

2018-11-22 09:15:45

Linux命令进程

2021-11-05 07:47:55

API计算任务

2024-05-10 08:44:53

C#软件开发优化代码

2024-07-03 13:51:02

SQL毛刺数据库

2023-01-27 15:28:04

开发Python内存

2020-12-25 08:52:53

SQLMysql 数据库

2012-01-10 10:44:36

字符串
点赞
收藏

51CTO技术栈公众号