Windows和OSX之间的iostream控制台输出的性能差异? [英] Performance difference of iostream console output between Windows and OSX?

查看:82
本文介绍了Windows和OSX之间的iostream控制台输出的性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下非常简单的for循环:

given the following very simple for loop:

int main (void) {
    for (int i = 0 ; i < 1000000; i++) {
         std::cout<<i<<std::endl;
    }
}

使用Microsoft在干净的Windows 8 Professional上运行此代码Visual Studio 2012每打印10万张纸大约要花费15秒。

running this code on a clean Windows 8 professional using Microsoft visual studio 2012 takes about 15 secs for every 100k prints.

在mac os x上,使用同一台计算机,xcode只需花费3秒即可输出1线。

On mac os x , using the same computer , it takes barely 3 secs for xcode to output 1 mill lines.

我几乎100%地确定它与性能无关,只是与输出机制有关的东西。

I'm almost 100% sure that it has nothing with the performance and it is just something that is related to the output mechanics or something.

有人可以确认这一点吗?只知道我的窗户&

Can someone confirm this..? just to know that my windows & visual studio are fine.

推荐答案

std :: endl 刷新行。

尝试做:

std::cout << i << '\n';




在大多数其他常见的交互式I / O方案中,std :: endl与std :: cout一起使用时是多余的,因为任何来自std :: cin的输入,输出到std :: cerr或程序终止都会强制调用std :: cout.flush()。

In most other usual interactive I/O scenarios, std::endl is redundant when used with std::cout because any input from std::cin, output to std::cerr, or program termination forces a call to std::cout.flush().

在某些消息源的鼓励下,使用 std :: endl 代替'\n'可能会严重降低输出性能。

Use of std::endl in place of '\n', encouraged by some sources, may significantly degrade output performance.

来源

编辑:
输出操作成本高昂且依赖在外部因素上。这就是为什么这里很慢。例如,正在使用的终端应用程序可能是一些性能问题的原因。

EDIT : Output operation are costly and depend on external factors. This is why it is slow here. For example, the terminal application being used can be factor of some performance issues.

您可以通过将输出重定向到 / dev / null来避免这种情况。 /

You can avoid that by redirecting the output to /dev/null/ :

./a.out > /dev/null

关于输出性能,您可以阅读以下内容: http://codeforces.com/blog/entry/5217

On output performance, you can read this : http://codeforces.com/blog/entry/5217

这篇关于Windows和OSX之间的iostream控制台输出的性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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