测试C语言中打印一句 hello world需要耗费多少时间 | 您所在的位置:网站首页 › 运行时间函数c语言是什么 › 测试C语言中打印一句 hello world需要耗费多少时间 |
文章目录
一、背景二、实现1.测试打印字符串2.测试打印整型3.测试打印字符串 + 整型
三、总结
一、背景
在实时操作系统上进行软件开发时,我们经常会加一些打印来进行调试,但有时会发生周期任务超时等问题,因此我们需要知道我们增加的打印(最终运行环境是不加打印的)对我们的周期任务耗时有什么影响,更具体一点,执行这些打印会耗费多少时间。 二、实现在C语言中,可以通过C语言系统调用方法,所需头文件ctime/time.h,即windows和linux都可以使用。 1.测试打印字符串下面是测试代码 #include #include #include 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); }执行结果如下图 可以看到,在测试电脑上,一秒是被分成了1000个时间片,时间测量最小单位为 1 ms。 因为只打印一句 hello world 所需时间很短,因此显示执行时间为 0 。 上面是在Windows电脑下测试的结果,我也把相同的代码放到我的linux服务器上去泡泡看看效果,结果如下 可以看到,linux下,一秒被分成了 1000000 个时间片,总耗时为 67 us,打印一次所耗时为 6.7 us,为啥时间少怎么多,很奇怪,我继续测试一下打印一次的时间,结果如下: 这个结果和我们在Windows上测试的结果是一致的。 下面我们增加一下打印次数来看看,测试代码如下 #include #include #include 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); }打印10次所耗费的时间为 1 ms ,故打印1次所耗费的时间为 100 us。 需要注意的是,经过我多次测试,有时候打印总耗时有时不是 0.001,有时是 0.002。 因此我们要明确,程序执行所耗时是一个很不确定的值,受很多因素的影响。 但是其大致量级是可以确定的,即在 100 us 左右。 2.测试打印整型上面测试打印的 hello world 是字符串,下面我们测试一下打印整型的情况。 测试代码如下 #include #include #include 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 #include #include 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左右,因此如果在某个任务中加入过多的打印,累积起来对该任务的执行耗时还是有不小的影响,这个我们在测试的过程中要心里有数。 |
CopyRight 2018-2019 实验室设备网 版权所有 |