std::endl may cause an unnecessary flushstd::endl is used instead of the more efficient
\n
This defect flags uses of std::endl in I/O operations and allows you to
use the more efficient alternative, \n.
std::endl inserts a newline (\n) followed by a
flush operation. For
instance:
std::cout << "Some content" << std::endl;
std::cout << "Some content" << '\n' << std::flush;
std::endl, the implicit flush operation can
significantly reduce program performance. Since the flush operation is implicit, in case of
a performance issue, it will be difficult to track the root cause of the issue.Use \n to enter a newline wherever possible.
If you require a flush operation, instead of std::endl, use
\n followed by an explicit flush operation, for
instance:
std::cout << "Some content" << '\n' << std::flush;
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
STD_ENDL_USE |
| Impact: Low |