为什么scanf / printf比cin / cout快? [英] Why is scanf/printf faster than cin/cout?

查看:102
本文介绍了为什么scanf / printf比cin / cout快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些程序,看到 scanf printf 比使用 cin cout ?当使用 scanf / printf

h2_lin>解决方案

在OP解释后编辑:这个答案不是这个问题,OP意味着执行时间限制。



你说清除在线编译器的时间限制,这让我想起你真的要求是为什么使用cin / cout 的程序编译比使用scanf / printf的程序慢。 / p>

原因是因为< iostream>,声明cin / cout的头部大量使用模板类,通常对于编译器解析和生成更复杂代码。因此,编译通常需要更长时间。当然,实际的性能取决于编译器,但更多的时候,编译大量模板化的代码需要一些时间。



这是一个页面,描述了编译C ++模板类所涉及的一些复杂性。



如果这不是你的意思,抱歉。


I've made some programs and saw that scanf and printf are considerably faster than using cin and cout? Most of my programs clear the execution time limit, mostly 3 seconds or 5 seconds, on online compilers when using scanf/printf which exceeded the limit while using cin/cout.

解决方案

Edited after OP clarified: This answer is not for this question, the OP meant execution time limits. Unless this answer starts getting hit with down votes, I'm going to leave it here anyways in case somebody finds it useful.

You say "clear the time limit on online compilers" which makes me think what you're really asking is why do programs that use cin/cout compile slower than programs that use scanf/printf.

The reason is because <iostream>, the header that declares cin/cout, makes heavy use of template classes, which generally are more complex for the compiler to parse and generate code for. Hence, compilation usually takes longer. The actual performance, of course, depends on the compiler, but more often than not, compiling heavily templated code takes some time.

Here is a page that describes some of the intricacies involved with compiling C++ template classes.

Apologies if this is not what you meant.

这篇关于为什么scanf / printf比cin / cout快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆