问题描述
我听说的snprintf比ostringstream更快。有没有人有任何经验?如果是的话,为什么它速度更快。
I read somewhere that snprintf is faster than ostringstream. Has anyone has any experiences with it? If yes why is it faster.
推荐答案
的std :: ostringstream
未要求,以比较慢,但是它一般是慢实现时。 的。
std::ostringstream
is not required to be slower, but it is generally slower when implemented. FastFormat's website has some benchmarks.
标准库的设计,流支持大于的snprintf
做得更多。该设计是为了可扩展的,并且包括被公开曝光的方法称为保护
虚拟
方法。这使您可以从流类之一派生,以保证,如果你超载保护
ED方法,你会得到你想要的行为。我相信,一个编译器能够避免虚拟
函数调用的开销,但我不知道,做任何的编译器。
The Standard library design for streams supports much more than snprintf
does. The design is meant to be extensible, and includes protected
virtual
methods that are called by the publicly exposed methods. This allows you to derive from one of the stream classes, with the assurance that if you overload the protect
ed method you will get the behavior you want. I believe that a compiler could avoid the overhead of the virtual
function call, but I'm not aware of any compilers that do.
此外,流操作经常使用可扩展的缓冲区内;这意味着相对较慢的内存分配。
Additionally, stream operations often use growable buffers internally; which implies relatively slow memory allocations.
这篇关于为什么的snprintf速度比ostringstream或者是什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!