Why is n++ faster than n=n+1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
1st Sep 2019, 5:54 PM
Eric Paul Owuor
Eric Paul Owuor - avatar
9 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'.
1st Sep 2019, 6:38 PM
Ipang
+ 5
Yea... no, it generates the exact same assembly with or without optimizations on pretty much all compilers. https://godbolt.org/z/V00PWk
1st Sep 2019, 9:05 PM
Dennis
Dennis - avatar
+ 4
Badgehog?
1st Sep 2019, 7:48 PM
HonFu
HonFu - avatar
+ 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.
1st Sep 2019, 9:36 PM
Miquel Andreu Fuster Sancho
Miquel Andreu Fuster Sancho - avatar
+ 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.
2nd Sep 2019, 7:21 AM
Ipang
+ 2
Ipang lmao. I thinks he's just curious
1st Sep 2019, 7:50 PM
Minerals2016
Minerals2016 - avatar
+ 2
And tbh I thinks it's a valid question cause I've heard that too. Maybe I'm wrong 🤔🤔🤔
1st Sep 2019, 7:50 PM
Minerals2016
Minerals2016 - avatar
+ 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
1st Sep 2019, 8:21 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
Cbr✔[ Exams ] Yeah, I guess so too, just wanted to confirm some things though 😁 Best of luck with exams! 👍
2nd Sep 2019, 7:25 AM
Ipang