stackoverflow : Why C++ output is too much slower than C?
来源:互联网 发布:数据分析师知乎 编辑:程序博客网 时间:2024/05/18 00:29
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, whilefprintf
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 。
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 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 OpenCV is slower than Matlab in matrix related operation,如:特征值分解,奇异值分解
- C expert: Too Much Default Visibility
- 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 VMWare runs much FASTer than Bochs?
- Why is processing a sorted array faster than an unsorted array(Stackoverflow)
- PHP is much better than you think
- where there is just too much work
- Intellij Idea 控制台“too much output to process”
- Why is volatile needed in c?
- Hey, C Is a Functional Language Too!
- Why Git is Better than X
- Why Git is Better than X
- Why our website is faster than yours
- Why is epoll faster than select?
- 写出float x 与“零值”比较的if语句
- 游戏开发技术总结(经典之作)第十三集 利用消息截获函数获取按键和游戏地图资源编辑器
- phpcms v9 后台登陆验证码无法显示
- GNU Gatekeeper 3.3 发布,网关守护管理
- Android加载网页JavaScript与Java之间的相互调用
- stackoverflow : Why C++ output is too much slower than C?
- hdu 4545 魔法串
- 2013 电影
- 黑马韩前成linux从入门到精通の3分钟搞定VPN服务器
- (并查集+欧拉通路)
- Win7笔记本电脑启用虚拟WIFI共享上网(转)
- Crawler中的问题
- PHPCMS V9“密码重试次数太多,请过-xxx分钟后重新登录!”的解决办法
- 汇编语言DEBUG命令大全