测试C语言中打印一句 hello world需要耗费多少时间

一、背景

在实时操作系统上进行软件开发时,我们经常会加一些打印来进行调试,但有时会发生周期任务超时等问题,因此我们需要知道我们增加的打印(最终运行环境是不加打印的)对我们的周期任务耗时有什么影响,更具体一点,执行这些打印会耗费多少时间。

二、实现

在C语言中,可以通过C语言系统调用方法,所需头文件ctime/time.h,即windows和linux都可以使用。

1.测试打印字符串

下面是测试代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    clock_t begin, end;
    double cost;
    //开始记录
    begin = clock();
    /*待测试程序段*/
    printf("hello world1!\n");
    //结束记录
    end = clock();
    cost = (double)(end - begin)/CLOCKS_PER_SEC;
    printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

执行结果如下图

image-20210831224524162

可以看到,在测试电脑上,一秒是被分成了1000个时间片,时间测量最小单位为 1 ms。

因为只打印一句 hello world 所需时间很短,因此显示执行时间为 0 。

上面是在Windows电脑下测试的结果,我也把相同的代码放到我的linux服务器上去泡泡看看效果,结果如下

image-20210831230613885

可以看到,linux下,一秒被分成了 1000000 个时间片,总耗时为 67 us,打印一次所耗时为 6.7 us,为啥时间少怎么多,很奇怪,我继续测试一下打印一次的时间,结果如下:

image-20210831231129313

这个结果和我们在Windows上测试的结果是一致的。

下面我们增加一下打印次数来看看,测试代码如下

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    clock_t begin, end;
    double cost;
    //开始记录
    begin = clock();
    /*待测试程序段*/
    printf("hello world1!\n");
    printf("hello world2!\n");
    printf("hello world3!\n");
    printf("hello world4!\n");
    printf("hello world5!\n");
    printf("hello world6!\n");
    printf("hello world7!\n");
    printf("hello world8!\n");
    printf("hello world9!\n");
    printf("hello world0!\n");
    //结束记录
    end = clock();
    cost = (double)(end - begin)/CLOCKS_PER_SEC;
    printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210831224855467.png

打印10次所耗费的时间为 1 ms ,故打印1次所耗费的时间为 100 us。

需要注意的是,经过我多次测试,有时候打印总耗时有时不是 0.001,有时是 0.002。

因此我们要明确,程序执行所耗时是一个很不确定的值,受很多因素的影响。

但是其大致量级是可以确定的,即在 100 us 左右。


2.测试打印整型

上面测试打印的 hello world 是字符串,下面我们测试一下打印整型的情况。

测试代码如下

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    clock_t begin, end;
    double cost;
    int i = 1;
    //开始记录
    begin = clock();
    /*待测试程序段*/

    printf("%d\n", i);
    printf("%d\n", i);
    printf("%d\n", i);
    printf("%d\n", i);
    printf("%d\n", i);
    printf("%d\n", i);
    printf("%d\n", i);
    printf("%d\n", i);
    printf("%d\n", i);
    printf("%d\n", i);
    //结束记录
    end = clock();
    cost = (double)(end - begin)/CLOCKS_PER_SEC;
    printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

在这里插入图片描述

可以看出和打印字符串的耗时是在同一量级的。


3.测试打印字符串 + 整型

下面我们测试一下在实际过程中我们经常打印的情况。

同时打印字符串和整型

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    clock_t begin, end;
    double cost;
    int i1 = 1;
    //开始记录
    begin = clock();
    /*待测试程序段*/

    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);
    printf("The number is %d\n", i1);

    //结束记录
    end = clock();
    cost = (double)(end - begin)/CLOCKS_PER_SEC;
    printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);

运行结果如下
在这里插入图片描述

打印字符串和整型和单独打印字符串和整型耗费时间量级基本一致。

三、总结

通过上面我们的测试,我们可以知道在C语言中打印语句printf的执行耗时在 0.1ms左右,因此如果在某个任务中加入过多的打印,累积起来对该任务的执行耗时还是有不小的影响,这个我们在测试的过程中要心里有数。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小熊coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值