i ++比++ i效率低,怎么显示呢? [英] i++ less efficient than ++i, how to show this?
问题描述
我试图通过示例显示前缀增量比后缀增量更有效。
I am trying to show by example that the prefix increment is more efficient than the postfix increment.
在理论上这是有道理的:i ++需要能够返回未增加的原始值,因此存储它,而++ i可以返回增加的值,而不存储以前的值。
In theory this makes sense: i++ needs to be able to return the unincremented original value and therefore store it, whereas ++i can return the incremented value without storing the previous value.
但是有一个很好的例子, ?
But is there a good example to show this in practice?
我试过下面的代码:
int array[100];
int main()
{
for(int i = 0; i < sizeof(array)/sizeof(*array); i++)
array[i] = 1;
}
我使用gcc 4.4.0编译它,如下:
I compiled it using gcc 4.4.0 like this:
gcc -Wa,-adhls -O0 myfile.cpp
我这样做了,后缀增量更改为前缀增量:
I did this again, with the postfix increment changed to a prefix increment:
for(int i = 0; i < sizeof(array)/sizeof(*array); ++i)
在这两种情况下,结果都是相同的汇编代码。
The result is identical assembly code in both cases.
这有点意外。看起来像是通过关闭优化(用-O0),我应该看到一个差异来显示的概念。我缺少什么?
This was somewhat unexpected. It seemed like that by turning off optimizations (with -O0) I should see a difference to show the concept. What am I missing? Is there a better example to show this?
推荐答案
在一般导致其中预增量不会的副本。当然,这将在大量的情况下被优化,并且在不是复制操作的情况下将被忽略(即,对于内置类型)。
In the general case, the post increment will result in a copy where a pre-increment will not. Of course this will be optimized away in a large number of cases and in the cases where it isn't the copy operation will be negligible (ie., for built in types).
这是一个小例子,显示后增量的潜在无效率。
Here's a small example that show the potential inefficiency of post-increment.
#include <stdio.h>
class foo
{
public:
int x;
foo() : x(0) {
printf( "construct foo()\n");
};
foo( foo const& other) {
printf( "copy foo()\n");
x = other.x;
};
foo& operator=( foo const& rhs) {
printf( "assign foo()\n");
x = rhs.x;
return *this;
};
foo& operator++() {
printf( "preincrement foo\n");
++x;
return *this;
};
foo operator++( int) {
printf( "postincrement foo\n");
foo temp( *this);
++x;
return temp;
};
};
int main()
{
foo bar;
printf( "\n" "preinc example: \n");
++bar;
printf( "\n" "postinc example: \n");
bar++;
}
优化版本的结果由于RVO的后增量情况):
The results from an optimized build (which actually removes a second copy operation in the post-increment case due to RVO):
construct foo()
preinc example:
preincrement foo
postinc example:
postincrement foo
copy foo()
一般来说,如果你不需要post-increment的语义,为什么会有不必要的复制的机会?
In general, if you don't need the semantics of the post-increment, why take the chance that an unnecessary copy will occur?
当然,记住一个自定义运算符++() - 前者或后变量 - 可以自由地返回任何它想要的(或甚至做任何它想要的),并且我想象有相当一个少数不遵循通常的规则。偶尔,我遇到了返回 void
的实现,这使得通常的语义差异消失。
Of course, it's good to keep in mind that a custom operator++() - either the pre or post variant - is free to return whatever it wants (or even do whatever it wants), and I'd imagine that there are quite a few that don't follow the usual rules. Occasionally I've come across implementations that return "void
", which makes the usual semantic difference go away.
这篇关于i ++比++ i效率低,怎么显示呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!