I read somewhere that snprintf is faster than ostringstream. Has anyone has any experiences with it? If yes why is it faster.
I read somewhere that snprintf is faster than ostringstream. Has anyone has any experiences
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
std::ostringstreamis not required to be slower, but it is generally slower when implemented. FastFormat’s website has some benchmarks.The Standard library design for streams supports much more than
snprintfdoes. The design is meant to be extensible, and includesprotectedvirtualmethods 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 theprotected method you will get the behavior you want. I believe that a compiler could avoid the overhead of thevirtualfunction 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.