C Programming: difference between ++i and i=i+1 from an assembler point of view?

后端 未结 6 500
予麋鹿
予麋鹿 2021-02-05 11:53

This was an interview question. I said they were the same, but this was adjudged an incorrect response. From the assembler point of view, is there any imaginable difference? I h

6条回答
  •  误落风尘
    2021-02-05 12:17

    The interviewer may have wanted an answer something like this:

    i=i+1 will have to load the value of i, add one to it, and then store the result back to i. In contrast, ++i may simply increment the value using a single assembly instruction, so in theory it could be more efficient. However, most compilers will optimize away the difference, and the generated code will be exactly the same.

    FWIW, the fact that you know how to look at assembly makes you a better programmer than 90% of the people I've had to interview over the years. Take solace in the fact that you won't have to work with the clueless loser who interviewed you.

提交回复
热议问题