From 33b3aa64d07a0f55e06534cebe62a5d489336660 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 28 Jun 2015 19:58:58 -0600 Subject: [PATCH] Make note of "" vs '' --- 08-Considering_Performance.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/08-Considering_Performance.md b/08-Considering_Performance.md index d733a5a..992b247 100644 --- a/08-Considering_Performance.md +++ b/08-Considering_Performance.md @@ -281,3 +281,16 @@ Even if many modern compilers will optimize these two loops to the same assembly 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. 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. +### Char is a char, string is a string + +```cpp +// Bad Idea +std::cout << someThing() << "\n"; + +// Good Idea +std::cout << someThing() << '\n'; +``` + +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. + +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.