您的位置:首页 > 编程语言 > C语言/C++

stackoverflow : Why C++ output is too much slower than C?

2013-05-17 21:47 609 查看


ANSWER:


It's likely because of how often you are flushing the stream to disk in the C++ code. Inserting
endl
into a stream inserts a new line and flushes the buffer, while
fprintf
doesn't cause a buffer flush.

So your C++ example performs 20,000,000 buffer flushes while your C example will only flush to disk when the file handles buffer is full.



知识点:C++里面endl用来换行的时候,会刷新缓冲区,将数据写入磁盘,从而造成了效率的损失,而C中的fprintf函数则不会。解决方法是用 \n 代替 endl 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: