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 。
原文链接: https://www.cnblogs.com/MuyouSome/archive/2013/05/17/3084452.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/88887
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!