1

i have a question, we know that ++i is faster to use as a iterator in for loop for classes such as list, because compiler doesn't create a temp object to store previous value. But, does that work also for int? I mean is ++i faster to use than i++ in for loop with int?

for(auto it = myList.begin(); it != myList.end(); ++it) {} - faster
for(auto it = myList.begin(); it != myList.end(); it++) {} - slower


for(int i=0; i<SIZE; ++i) {} - faster?
for(int i=0; i<SIZE; i++) {} - slower?
Yashmerino
  • 121
  • 9
  • 9
    It will almost certainly be optimised to the same thing. The only way to be sure would be to look at the generated assembly for your real code with your real compiler settings, but I'd bet on it. – BoBTFish Aug 17 '21 at 11:04
  • 1
    very unlikely `++i` will be slower than `i++`, so why not choose consistency over premature optimization ;) – 463035818_is_not_an_ai Aug 17 '21 at 11:08
  • @463035818_is_not_a_number why would it be slower? You don't need to remember the previous value. – Yashmerino Aug 17 '21 at 11:10
  • 1
    btw "we know that ++i is faster". I am not so sure about that. Even with iterators the compiler knows that the value of the expression is ignored. – 463035818_is_not_an_ai Aug 17 '21 at 11:10
  • 1
    @Yashmerino I didnt say that it will be slower. I said it is very unlikely to be slower – 463035818_is_not_an_ai Aug 17 '21 at 11:11
  • 2
    Even the two iterator loops could be optimized to be the same. While highly simplified [this example](https://godbolt.org/z/hn7absaba) shows the exact same code being generated for both loops. – Some programmer dude Aug 17 '21 at 11:11
  • 1
    in any case, what about this question cannot be answered by running a benchmark and/or looking at compilers output? Anything else is just speculations – 463035818_is_not_an_ai Aug 17 '21 at 11:12
  • @463035818_is_not_a_number oh, i'm so sorry, thank you for the answer – Yashmerino Aug 17 '21 at 11:12
  • @463035818_is_not_a_number i found a test with iterators where ++i is faster: Release: iterator++. Total time: 0.87779 ++iterator. Total time: 0.87753 Debug: iterator++. Total time: 83.2849 ++iterator. Total time: 27.1557. So yeah, i'm gonna test it with int, it would be the best answer :) – Yashmerino Aug 17 '21 at 11:16
  • 1
    @463035818_is_not_a_number a decent compiler knows that, but I have to use some mainstream compiler of a big company I won't name here, and I'm quite often surprised how dumb it is. But to support your "so why not choose consistency over premature optimization": the default should be pre-increment, because it's sometimes faster and very unlikely to be slower and for the sake of consistency I'd say "just pick ++i except when you know you need i++" – Stefan Riedel Aug 17 '21 at 11:18
  • 2
    Really no offense, but the phrase "as we all know" often means "I have zero evidence, but I hope you have the same preoccupation", also here. There are good reasons to expect it to be faster in some cases, but it isnt always faster. – 463035818_is_not_an_ai Aug 17 '21 at 11:19
  • 1
    @StefanRiedel "the default should be pre-increment, because it's sometimes faster...." exactly. Imho "we know that ++i is faster" is too much simplified and leads to misunderstandings. Even in cases `++i` is not faster, there a good reasons to prefer it – 463035818_is_not_an_ai Aug 17 '21 at 11:21
  • @463035818_is_not_a_number It's ok, i just thought it is always likely to be faster, if i'm not right, i'm sorry. I'm just a beginner :) – Yashmerino Aug 17 '21 at 11:21
  • 3
    Debug builds are usually never optimized. When measuring and benchmarking you should always look at optimized code. And as you quote, the (optimized) release build have virtually no difference between the two loops. – Some programmer dude Aug 17 '21 at 11:22
  • 3
    we are all beginners (just at different levels of beginnerness ;). I thought it is worth to clarify, no need to be sorry, all fine :) – 463035818_is_not_an_ai Aug 17 '21 at 11:22
  • ahhaha, the phrase "at different levels of beginnerness" is the best i've ever heard!) Thank you for the help guys, appreciate it – Yashmerino Aug 17 '21 at 11:23
  • 5
    Performance questions always have the same answer: profile, profile, profile. (With optimizations enabled.) – Eljay Aug 17 '21 at 11:36

0 Answers0