Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
使用一个非常基本的示例:
for(int i = 0; i < value; i++)
我使用 i++ 还是 ++i 有什么区别吗?我不会在 i 递增时读取它,因此前缀或后缀无关紧要,但是在程序效率或编译器优化等方面是否存在差异?
没有任何。
int在某些语言中,某些类型的后缀和前缀的性能可能会有所不同,但对于像不使用该值时通常会优化掉的内置函数则不会。在某些语言中,您可能会发现doSomething(i); i++或doSomething(i); ++i稍微快一点,doSomething(i++);因为临时变量没有得到优化。
int
doSomething(i); i++
doSomething(i); ++i
doSomething(i++);
这种情况比较少见,影响不大。
但是,在这里,对于该代码有效的几乎任何语言,它实际上都不会产生任何影响。