Why does using std::endl with ostringstream affect output speed?

前端 未结 3 1686
执念已碎
执念已碎 2021-02-15 10:27

I\'m timing the difference between various ways to print text to standard output. I\'m testing cout, printf, and ostringstream using both

3条回答
  •  天命终不由人
    2021-02-15 10:43

    Using std::endl is equivalent to writing

    stream << "\n";
    stream.flush();
    

    Don't use std::endl unless you actually want to trigger flushing, and / or don't care about output performance.

    Also, don't worry about different line endings on different platforms, your implementation will translate "\n" to the line ending appropriate for your platform.

提交回复
热议问题