龙空技术网

Linux下如何计算函数的执行时间?实例代码直接拿去用

一口Linux 3188

前言:

今天我们对“linux 函数”大约比较关心,兄弟们都需要学习一些“linux 函数”的相关资讯。那么小编同时在网上收集了一些有关“linux 函数””的相关资讯,希望大家能喜欢,兄弟们快快来了解一下吧!

关于时间操作的文章,大家还可以可以关注我之前的一篇文章《C语言操作时间函数time.ctime,实现定时执行某个任务小例子》

0、问题描述

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

一、问题分析

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

程序内计算一个函数的执行时间计算一个程序的执行时间二、程序内如何计算一个函数的执行时间?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会被赋值

因为我们希望计算执行某个函数的时间,所以我们第一个参数选择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 }

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

编译

gcc runtime.c -lrt

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

执行结果如下:

root@ubuntu:/home/peng/zhh# ./a.out you can call your function hereCLOCK_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 <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 }

编译执行如下:

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

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

root@ubuntu:/home/peng/zhh# time ./a.out ** Total time 0s + 9649603 nsec, avg_time = -9649603.000000 real 0m0.010suser 0m0.000ssys     0m0.000s

下面我们将59行代码中的usleep(200)修改成sleep(1) 重新编译执行,10秒回打印如下执行结果:

root@ubuntu:/home/peng/zhh# time ./a.out ** Total time 10s + 8178015 nsecreal 0m10.009suser 0m0.000ssys  0m0.000s

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

想学习更多的嵌入式知识和技巧,请关注一口君:一口Linux

标签: #linux 函数 #linux怎么计算