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

0、问题描述
粉丝想计算一个函数的执行时间。
一、问题分析
函数的执行时间的统计在嵌入式系统中会被频繁的用到,知识点很重要。本文从两个方面来讨论类似的问题:
二、程序内如何计算一个函数的执行时间?
1. 思路
我们在执行函数前后分别记录下时间戳,然后计算两个时间戳的差值即可。
我们需要借助函数clock_gettime来实现这个功能。看下该函数的定义:
- #include
 - 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
 - 2 #include
 - 3 #include
 - 4 #include
 - 5 #include
 - 6 #include
 - 7 #include
 - 8 #include
 - 9 #include
 - 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
 - 2 #include
 - 3 #include
 - 4 #include
 - 5 #include
 - 6 #include
 - 7 #include
 - 8 #include
 - 9 #include
 - 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
 
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
 
结果和预期基本一致。
大家可以根据我的代码,方便的将该功能移植到自己的项目中。
            
            名称栏目:如何计算函数的执行时间?            
            分享网址:http://www.scanyue.cn/article/cdiodhp.html