问题描述
我一直想知道printf和cout ..更快,也是最灵活的(即可以打印一系列变量,并且输出可以格式化)?
I've always wondered about printf and cout.. which one is ultimately faster, and is it the most flexible as well (ie can print a range of variables, and output can be formatted)?
PS
我知道这看起来类似于)。
The problem with the stream is that they are quite difficult to handle correctly when wanting to specify format of some data (padding spaces for numbers, for example), and that sometimes, you really really need to go fast. Then, either fall back to printf, or try some high-speed C++ alternatives (FastFormat comes to mind).
编辑:请注意,系列测试显示了有趣的结果(我现在在我的电脑上复制),即: cout
和 printf
有类似的性能,当避免使用 std :: endl
输出 \\\
)。
Note that Thomas' series of tests show interesting results (which I reproduced right now on my computer), that is: cout
and printf
have similar performances when one avoids using std::endl
(which flushes the output in addition to outputing a \n
).
这篇关于这更快,而且更灵活:printf或cout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!