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
endlinto a stream inserts a new line and flushes the buffer, while
fprintfdoesn'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 。
相关文章推荐
- stackoverflow : Why C++ output is too much slower than C?
- stackoverflow : Why C++ output is too much slower than C?
- Why is one loop so much slower than two loops?
- Why AlloyFinger is so much smaller than hammerjs?
- Why is processing a sorted array faster than an unsorted array(Stackoverflow)
- Why is FSB Slower than CPU internal speed?
- Why OpenCV is slower than Matlab in matrix related operation,如:特征值分解,奇异值分解
- Why is processing a sorted array faster than an unsorted array(Stackoverflow)
- Why iperf dual traffic in 100Mbps envrionment is much less than 100M?
- keras: Why is the training loss much higher than the testing loss?
- Why is a c++ reference considered safer than a pointer?
- Why malloc+memset is slower than calloc?
- 《What is deep learning and why is it getting so much attention?》,译名:深度学习的定义及其备受关注的原因
- Why ConcurrentHashMap is better than Hashtable and just as good as a HashMap
- Why Free Software Is More Important Now Than Ever Before
- Why Git is Better than X
- why is the output of `du` often so different from
- Why is using BufferedInputStream to read a file byte by byte faster than using FileInputStream
- Why MicroStrategy proferssioanl are paid too much (comapred to BO, Cognos, etc)
- Why Git is Better than X