mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 20:54:41 +03:00
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:
committed by
Herb Sutter
parent
d456ccf274
commit
1d54448aeb
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -564,6 +564,7 @@ tmp
|
|||||||
TMP
|
TMP
|
||||||
tock
|
tock
|
||||||
TODO
|
TODO
|
||||||
|
toLower
|
||||||
toolchains
|
toolchains
|
||||||
TotallyOrdered
|
TotallyOrdered
|
||||||
TP
|
TP
|
||||||
|
|||||||
Reference in New Issue
Block a user