Editing P.9: "Don't waste time or space" Example Text (#1439)

* Initial rewrite

* Fixed a couple of inaccuracies and minor grammar mistakes

Thanks to twitter user @lunasorcery for these changes!

* Added toLower to dictionary
This commit is contained in:
Joshua T. Fisher
2019-06-20 11:12:29 -07:00
committed by Herb Sutter
parent d456ccf274
commit 1d54448aeb
2 changed files with 3 additions and 2 deletions

View File

@@ -1055,8 +1055,8 @@ There are several more performance bugs and gratuitous complication.
for (int i = 0; i < strlen(s); ++i) s[i] = tolower(s[i]); for (int i = 0; i < strlen(s); ++i) s[i] = tolower(s[i]);
} }
Yes, this is an example from production code. This is actually an example from production code.
We leave it to the reader to figure out what's wasted. We can see that in our condition we have `i < strlen(s)`. This expression will be evaluated on every iteration of the loop, which means that `strlen` must walk through string every loop to discover its length. While the string contents are changing, it's assumed that `toLower` will not affect the length of the string, so it's better to cache the length outside the loop and not incur that cost each iteration.
##### Note ##### Note

View File

@@ -564,6 +564,7 @@ tmp
TMP TMP
tock tock
TODO TODO
toLower
toolchains toolchains
TotallyOrdered TotallyOrdered
TP TP