订阅
纠错
加入自媒体

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

2021-02-24 14:20
一口Linux
关注

编译

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.",
  (long long) t1->tv_sec,t1->tv_nsec);
 return;

if (timespec_check(t2) < 0) {
 fprintf(stderr, "invalid time #2: %lld.%.9ld.",
  (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.",
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.",
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",
69                 ts_end.tv_sec, ts_end.tv_nsec);    
70         #endif
71     }
72     printf("** Total time %lds + %ld nsec",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. 例11. 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
在以上实例中,执行命令"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.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

结果和预期基本一致。

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


<上一页  1  2  
声明: 本文由入驻维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。

发表评论

0条评论,0人参与

请输入评论内容...

请输入评论/评论长度6~500个字

您提交的评论过于频繁,请输入验证码继续

暂无评论

暂无评论

人工智能 猎头职位 更多
扫码关注公众号
OFweek人工智能网
获取更多精彩内容
文章纠错
x
*文字标题:
*纠错内容:
联系邮箱:
*验 证 码:

粤公网安备 44030502002758号