是std :: copy比std :: cout更快的输出? [英] is std::copy faster than std::cout for output?

查看:63
本文介绍了是std :: copy比std :: cout更快的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  std :: vector< int>v {2,4,6,8,10,12,14,16,18,20};//打印数字std :: copy(v.cbegin(),v.cend(),std :: ostream_iterator< int>(std :: cout,")));std :: cout<<'\ n'; 

此处std :: copy用于写入标准输出.这比在for循环中对向量元素使用std :: cout更快

  for(自动元素:v)std :: cout<<元素<<"; 

我找不到有关它们如何写入两者的输出缓冲区的太多信息.

解决方案

要大致了解两者的相对性能,请在此处查看基准测试结果:

here std::copy is used to write to std out. Is this faster than using std::cout for the vector elements in a for loop like

for(auto element: v) std::cout << element << " ";

I could't find much information about how they would write to output buffers for both.

To give an rough idea on the relative performance of the two, see the benchmark results here: http://quick-bench.com/wGYYPBXEgvLrkyp5gpJOnIpt7A4

I had to output to a std::stringstream instead of std::cout to keep quick-bench happy. It gives some insight on the raw performance of the underlying implementations, but not on how they crossplay with a highly OS dependent output stream like std::cout.

So it is hard to come to any definitive conclusions based on such a simple benchmark alone. I would take from this that in reality there is most likely not enough difference between the two approaches to prefer one over the other from a performance perspective.

这篇关于是std :: copy比std :: cout更快的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆