为什么C语言中的前缀增量(++ x)比后缀增量(x ++)快? [英] Why is prefix incrementation (++x) faster than postfix incrementation (x++) in C?

查看:120
本文介绍了为什么C语言中的前缀增量(++ x)比后缀增量(x ++)快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我听说它在C语言中前缀增加快于后缀增加。这是真的,为什么?

I heard it said that prefix incrementation is faster than postfix incrementation in C. Is this true and why?

++ x vs x ++

++x vs x++

推荐答案

这是一个荒谬的神话,一遍又一遍地重复。这两个运算符具有语义差异;一个具有旧值,另一个具有新值。如果使用此结果,则代码将根据您使用的运算符而具有不同的行为,并且如果可以比另一行为更有效地实现一种行为,则这可能包括性能差异。但是如果不使用结果,则 x = x + 1 x + = 1 x ++ ++ x 相同

This is a ridiculous myth that gets repeated over and over. The two operators have semantic differences; one has as its result the old value and the other has as the result its new value. If you use this result, the code will have different behavior depending on which operator you used, and this may include performance differences if one behavior can be achieved more efficiently than the other. But if you don't use the result, x=x+1, x+=1, x++, and ++x are all identical.

这篇关于为什么C语言中的前缀增量(++ x)比后缀增量(x ++)快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆