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

上面是在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);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数软件测试工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上软件测试开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注软件测试)
img

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

加V获取:vip1024b (备注软件测试)**
[外链图片转存中…(img-7EoJEtRo-1713040994622)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 13
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值