C ++:cout语句会使代码变慢吗 [英] C++: Does cout statement makes code slower
问题描述
我正在从一个文件中读取大约300万行,并将其插入STL映射中.因此,在我从文件中读取每一行的while循环中,我还通过一个简单的cout语句打印以控制台其行号.我的一位朋友最近指出,这会使代码变慢.我想知道这是否是真的,是否是为什么?
I am reading about 3 million rows from a file and inserting them into STL maps. So, inside my while loop where I read each line from the file, I also print to console what row number it is through a simple cout statement. One of my friends recently pointed out that this makes code slower. I was wondering whether it is true and if it is why?
推荐答案
正如已经提到的,写入终端几乎肯定会比较慢.为什么?
As already mentioned, writing to the terminal is almost definitely going to be slower. Why?
-
取决于您的操作系统,
std :: cout
可能会使用行缓冲-这意味着每行可能会分别发送到终端程序.当您使用std :: endl
而不是'\ n'时,它肯定会刷新缓冲区.以较小的块写入数据意味着额外的系统调用和渲染工作,从而大大降低了运行速度.depending on your OS,
std::cout
may use line buffering - which means each line may be sent to the terminal program separately. When you usestd::endl
rather than '\n' it definitely flushes the buffer. Writing the data in smaller chunks means extra system calls and rendering efforts that slow things down significantly.某些操作系统/编译器甚至更慢-例如,Visual C ++:
some operating systems / compilers are even slower - for example, Visual C++: https://connect.microsoft.com/VisualStudio/feedback/details/642876/std-wcout-is-ten-times-slower-than-wprintf-performance-bug-in-c-library
终端需要调用以擦除现有屏幕内容,渲染字体,更新滚动条,将行复制到历史记录/缓冲区中.特别是当他们以小块的形式获得新内容时,他们无法可靠地猜测需要等待多长时间,并且可能会尝试更新屏幕以获得收到的一点点信息:这是昂贵的,并且过多的刷新或无缓冲的输出很慢的原因.
terminals displaying output need to make calls to wipe out existing screen content, render the fonts, update the scroll bar, copy the lines into the history/buffer. Especially when they get new content in small pieces, they can't reliably guess how much longer they'd have to wait for some more and are likely to try to update the screen for the little bit they've received: that's costly, and a reason excessive flushing or unbuffered output is slow.
-
某些终端提供跳转滚动"选项,这意味着如果发现它们说落后10页,它们将立即渲染最后一页,而之前的9页内容将永远不会出现在屏幕上:好又快.不过,跳动滚动"并不总是被使用或想要的,因为这意味着输出永远不会呈现给最终用户的眼睛:也许该程序在某些情况下会打印出巨大的红色错误消息-跳动滚动甚至都不会闪烁一下即可吸引用户的注意,但是如果不进行跳跃滚动,您可能会注意到它.
Some terminals offer the option of "jump scrolling" which means if they find they're say 10 pages behind they immediately render the last page and the earlier 9 pages of content never appear on the screen: that can be nice and fast. Still, "jump scrolling" is not always used or wanted, as it means output is never presented to the end users eyes: perhaps the program is meant to print a huge red error message in some case - with jump scrolling there wouldn't even be a flicker of it to catch the user's attention, but without jump scrolling you'd probably notice it.
当我在彭博社工作时,日志文件更新源源不断地占据着几台显示器-有时显示的输出会落后几分钟.从默认的Solaris xterm切换到rxvt可以确保始终保持同步
when I worked for Bloomberg we had a constant stream of log file updates occupying several monitors - at times the displayed output would get several minutes behind; a switch from the default Solaris xterm to rxvt ensured it always kept pace
将输出重定向到/dev/null是查看您的特定终端在多大程度上降低速度的好方法
redirecting output to /dev/null is a good way to see how much your particular terminal is slowing things down
这篇关于C ++:cout语句会使代码变慢吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-