From 395ccfaf721d10d76296a340bdf6540b056b01c4 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 28 Jun 2015 13:39:44 -0600 Subject: [PATCH] Minor wording changes to iterator discussion @rob100 I changed the wording about iterators and `++` because many iterator types are just internally a `++pointer` and the compiler *is* able to optimize it to the same level as `++int`. But that's not guaranteed to be true. Closes #19 --- 08-Considering_Performance.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/08-Considering_Performance.md b/08-Considering_Performance.md index 505a45c..d733a5a 100644 --- a/08-Considering_Performance.md +++ b/08-Considering_Performance.md @@ -276,7 +276,8 @@ for (int i = 0; i < 15; ++i) std::cout << i << '\n'; } ``` -Even if many modern compilers will optimize this two loops to the same assembly code, it is still good practice to prefer `++i`. There is absolutely no reason not to and you can never be certain that your code will not pass a compiler that does not optimize this. -You should be also aware that the compiler will optimize this only for integer types and not for loops with iterators or objects with an overloaded ++operator. + +Even if many modern compilers will optimize these two loops to the same assembly code, it is still good practice to prefer `++i`. There is absolutely no reason not to and you can never be certain that your code will not pass a compiler that does not optimize this. +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.