C语言程序设计:从一个角度汇编点++ i和i = i + 1的区别? [英] C Programming: difference between ++i and i=i+1 from an assembler point of view?
问题描述
这是一个面试问题。我说他们是一样的,但是这被判定不正确的响应。但从汇编点,是否有任何可以想象的区别?我编使用默认的gcc的优化和-S看看汇编器输出的两个短的C程序,它们是相同的。
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 have compiled two short C programs using default gcc optimization and -S to see the assembler output, and they are the same.
推荐答案
面试官可能是想答案是这样的:
The interviewer may have wanted an answer something like this:
I = I + 1
将要载入 i的值
,添加一个,然后存储结果返回给 I
。相比之下, ++我
可以简单地使用单个汇编指令,所以理论上它可能是更有效的增加值。然而,大多数的编译器将优化程差,并将所产生的code将是完全一样的。
i=i+1
will have to load the value ofi
, add one to it, and then store the result back toi
. 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,你知道怎么看装配让你比我有采访多年来人们90%的更好的程序员的事实。以安慰的事实,你不会有谁面试你无能的失败者工作。
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.
这篇关于C语言程序设计:从一个角度汇编点++ i和i = i + 1的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!