printf比std :: cout快5倍以上?

编程入门 行业动态 更新时间:2024-10-27 02:18:21
本文介绍了printf比std :: cout快5倍以上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#include< iostream> #include< cstdlib> #include< cstdio> #include< ctime> int main(int argc,char * argv []) { std :: clock_t start; double duration; std :: cout<< 启动std :: cout测试。 << std :: endl; start = std :: clock(); for(int i = 0; i <1000; i ++) { std :: cout< Hello,World!(<< i<<)< std :: endl; } duration =(std :: clock() - start)/(double)CLOCKS_PER_SEC; std :: cout<< 结束std :: cout测试。 << std :: endl; std :: cout<< 所用时间:<持续时间< std :: endl; std :: system(pause); std :: cout<< 启动std :: printf测试。 << std :: endl; start = std :: clock(); for(int i = 0; i <1000; i ++) { std :: printf(Hello,World!(%i)\\\一世); std :: fflush(stdout); } duration =(std :: clock() - start)/(double)CLOCKS_PER_SEC; std :: cout<< Ending std :: printf test。 << std :: endl; std :: cout<< 所用时间:<持续时间< std :: endl; 系统(pause); return 0; }

现在,这里是前五次运行的时间:

  • std :: cout test: 1.125 s; printf测试: 0.195 s
  • std :: cout test: 1.154 printf测试: 0.230 s
  • std :: cout test: 1.142 printf测试: 0.216 s
  • std :: cout test: 1.322 printf测试: 0.221 s
  • std :: cout test: 1.108 printf test: 0.232 s

正如你所看到的,使用 printf 然后 fflush ing比使用 std :: cout 大约少5倍的时间。

虽然我希望使用 std :: cout 的< / code>运算符可能有点慢(几乎最小),我没有准备好这个巨大的差异。我做一个公平的测试?如果是这样,那么什么使第一个测试比第二个测试慢得多,如果他们基本上做同样的事情?

解决方案

对于真正的苹果对苹果比较,重写你的测试,以便在测试用例之间改变的只是使用的打印函数:

int main(int argc,char * argv []) { const char * teststring =Test output string\ std :: clock_t start; double duration; std :: cout<< 启动std :: cout测试。 << std :: endl; start = std :: clock(); for(int i = 0; i <1000; i ++) std :: cout<测试串 / *显示时间结果,代码为简洁修剪* / for(int i = 0; i <1000; i ++){ std :: printf(teststring) ; std :: fflush(stdout); } / *显示时序结果,代码为了简洁* / return 0; }

这样,你将只测试 printf 和 cout 函数调用。您不会因多个<< 电话等造成任何差异。如果您尝试这样做,我怀疑您会得到一个不同的结果。 p>

#include <iostream> #include <cstdlib> #include <cstdio> #include <ctime> int main(int argc, char* argv[]) { std::clock_t start; double duration; std::cout << "Starting std::cout test." << std::endl; start = std::clock(); for (int i = 0; i < 1000; i++) { std::cout << "Hello, World! (" << i << ")" << std::endl; } duration = (std::clock() - start) / (double) CLOCKS_PER_SEC; std::cout << "Ending std::cout test." << std::endl; std::cout << "Time taken: " << duration << std::endl; std::system("pause"); std::cout << "Starting std::printf test." << std::endl; start = std::clock(); for (int i = 0; i < 1000; i++) { std::printf("Hello, World! (%i)\n", i); std::fflush(stdout); } duration = (std::clock() - start) / (double) CLOCKS_PER_SEC; std::cout << "Ending std::printf test." << std::endl; std::cout << "Time taken: " << duration << std::endl; system("pause"); return 0; }

Now, here are the times for the first five runs:

  • std::cout test: 1.125 s ; printf test: 0.195 s
  • std::cout test: 1.154 s ; printf test: 0.230 s
  • std::cout test: 1.142 s ; printf test: 0.216 s
  • std::cout test: 1.322 s ; printf test: 0.221 s
  • std::cout test: 1.108 s ; printf test: 0.232 s

As you can see, using printf and then fflushing takes about 5 times less time than using std::cout.

Although I did expect using std::cout's << operator to be perhaps a little slower (almost minimal) , I wasn't prepared for this huge difference. Am I making a fair test? If so, then what makes the first test so much slower than the second one, if they essentially do the exact same thing?

解决方案

For a true apples-to-apples comparison, re-write your test so that the only thing changing between the test cases is the print function being used:

int main(int argc, char* argv[]) { const char* teststring = "Test output string\n"; std::clock_t start; double duration; std::cout << "Starting std::cout test." << std::endl; start = std::clock(); for (int i = 0; i < 1000; i++) std::cout << teststring; /* Display timing results, code trimmed for brevity */ for (int i = 0; i < 1000; i++) { std::printf(teststring); std::fflush(stdout); } /* Display timing results, code trimmed for brevity */ return 0; }

With that, you will be testing nothing but the differences between the printf and cout function calls. You won't incur any differences due to multiple << calls, etc. If you try this, I suspect that you'll get a much different result.

更多推荐

printf比std :: cout快5倍以上?

本文发布于:2023-10-18 11:37:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:printf   std   cout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!