std::endl may cause an unnecessary flush
std::endl is used instead of the more efficient
\n
Description
This defect flags uses of std::endl in I/O operations and allows you to
use the more efficient alternative, \n.
Risk
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.Fix
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.
Examples
Result Information
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
STD_ENDL_USE |
| Impact: Low |
Version History
Introduced in R2020a
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)