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

 

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

posted @ 2013-05-17 21:47  木有Some  阅读(432)  评论(0编辑  收藏  举报