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

前端 未结 3 1850
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 10:10

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:44

    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.

提交回复
热议问题