问题描述
我做了一些程序,看到 scanf
和 printf
比使用 cin
和 cout
?当使用 scanf
/ printf
在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快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!