+ 1
Why is n++ faster than n=n+1
10 Answers
+ 12
1. How did you come up with that conclusion?
2. Why are you linking someone else's code? if you have a problem with your code it's the one you should share, not the others'.
+ 5
Yea... no, it generates the exact same assembly with or without optimizations on pretty much all compilers.
https://godbolt.org/z/V00PWk
+ 4
Badgehog?
+ 3
++ and - was created in the first few days to take advantage of the INC and DEC instructions of the processor. These were faster than normal addition and subtraction by not having to execute so many microinstructions. By the way the preincrement was faster than the postincrement because the first one had to return the value before increasing.
Now, with the advances in the compilers, they recognize what the programmer wants to do and optimize the code, so whatever the instruction used, it generates the same object code.
+ 3
Minerals2016
Curiosity is a wonderful thing that brings better things in life buddy, I have no problem with curiosity, I love curious people š
Yep, it's quite a valid question, but when we ask why something is better/faster than another then it is expected of us to present the proof that lead us to think or believe it is so; lead to the doubt - thus the question arised. That proof is the missing link here : )
Unfortunately I couldn't find any comparison of the increment operations in the code attached (apart from it was other's code not the OP's). That's why I decided to confirm with the OP. In a hope good answers come to clear the doubt, given the fact (article or code) was presented.
+ 2
Ipang lmao. I thinks he's just curious
+ 2
And tbh I thinks it's a valid question cause I've heard that too. Maybe I'm wrong š¤š¤š¤
+ 2
ChillPill have carried out this test long time ago, which we notice that n=n+1; is faster than n+=1; but i can't really tell of n++ or ++n, maybe is should just look for the post
+ 2
Cbrā[ Exams ]
Yeah, I guess so too, just wanted to confirm some things though š
Best of luck with exams! š