8000 Make note of "" vs '' · misgeatgit/cppbestpractices@33b3aa6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 33b3aa6

Browse files
committed
Make note of "" vs ''
1 parent f0afb2d commit 33b3aa6

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

08-Considering_Performance.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,16 @@ Even if many modern compilers will optimize these two loops to the same assembly
281281
You should be also aware that the compiler will not be able optimize this only for integer types and not necessarily for all iterator or other user defined types.
282282
The bottom line is that it is always easier and recommended to use the pre-increment operator if it is semantically identical to the post-increment operator.
283283

284+
### Char is a char, string is a string
285+
286+
```cpp
287+
// Bad Idea
288+
std::cout << someThing() << "\n";
289+
290+
// Good Idea
291+
std::cout << someThing() << '\n';
292+
```
293+
294+
This is very minor, but a `"\n"` has to be parsed by the compiler as a `const char *` which has to do a range check for `\0` when writing it to the stream (or appending to a string). A '\n' is known to be a single character and avoids many CPU instructions.
295+
296+
If used inefficiently very many times it might have an impact on your performance, but more importantly thinking about these two usage cases gets you thinking more about what the compiler and runtime has to do to execute your code.

0 commit comments

Comments
 (0)
0