运算符 x++;和++x;对于 int. 哪个更快?为什么?

发布于 2024-12-14 19:42:25 字数 275 浏览 6 评论 0原文

可能的重复:
i++ 和 ++ 之间是否存在性能差异我在 C++ 中?

他们说 ++i 更快,但我不明白为什么。有人可以向我展示这些运算符的汇编代码吗?

Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?

They say that ++i is faster but I don't understand why.Can anybody show me assembler codes of these operators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-12-21 19:42:25

++i 绝对与 i++ 一样快,但它可能更快。
原因在于执行。

为了实现 i++,该实现需要生成 i 的临时副本,这与 ++i 的实现不同。

但智能编译器可以优化这个临时文件的生成,当然对于 POD 类型也是如此。

++i is definitiely as fast as i++ but it may be faster.
The reason is the implementation.

In order to implement i++ the implementation needs to generate a temporary copy of i unlike the implementation for ++i.

But smart compilers can optimize the genration of this temporary, they certainly do for POD types.

葮薆情 2024-12-21 19:42:25

是否为此表达式生成更快的代码取决于编译器和情况。

It depends on the compiler and the situation if it generates faster code for this expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文