是否有我的++我在C性能差异++和? [英] Is there a performance difference between i++ and ++i in C?
本文介绍了是否有我的++我在C性能差异++和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有 i ++
和 ++我
如果不使用所得到的值之间的性能差异?
Is there a performance difference between i++
and ++i
if the resulting value is not used?
推荐答案
摘要:否
我++
可能会慢于 ++我
,因为的旧值我
可能需要被保存以供以后使用,但在实践中所有的现代
编译器将优化这个了。
i++
could potentially be slower than ++i
, since the old value of i
might need to be saved for later use, but in practice all modern
compilers will optimize this away.
我们可以通过在code寻找这个功能证实这点,
均与 ++我
和我++
。
We can demonstrate this by looking at the code for this function,
both with ++i
and i++
.
$ cat i++.c
extern void g(int i);
void f()
{
int i;
for (i = 0; i < 100; i++)
g(i);
}
该文件是相同的,除了 ++我
和我++
:
$ diff i++.c ++i.c
6c6
< for (i = 0; i < 100; i++)
---
> for (i = 0; i < 100; ++i)
我们会编译它们,并且也得到了生成的汇编:
We'll compile them, and also get the generated assembler:
$ gcc -c i++.c ++i.c
$ gcc -S i++.c ++i.c
和我们可以看到,无论所生成的对象,并汇编文件是相同的。
And we can see that both the generated object and assembler files are the same.
$ md5 i++.s ++i.s
MD5 (i++.s) = 90f620dda862cd0205cd5db1f2c8c06e
MD5 (++i.s) = 90f620dda862cd0205cd5db1f2c8c06e
$ md5 *.o
MD5 (++i.o) = dd3ef1408d3a9e4287facccec53f7d22
MD5 (i++.o) = dd3ef1408d3a9e4287facccec53f7d22
这篇关于是否有我的++我在C性能差异++和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文