了解更多关于我++和i = i + 1 [英] Understanding more about i++ and i=i+1

查看:197
本文介绍了了解更多关于我++和i = i + 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有这两种形式的增量之间的差额。一些链接说我++是更快即I = I + 1;

I was wondering if there is difference between the two forms of increment. Some of the links says i++ is faster that i=i+1;

另外,作为人,我的观察也是组装$ C $相同c之一。请检查其中两个装配code是相同的图像我++和i = i + 1 -

Also as one of the person my observation is also the same for assembly code. please check the image where both the assembly code are same for i++ and i=i+1 -

有另一种链接,指出它曾经是真正的previously的增量运算比加法和分配速度更快,但现在编译器优化I ++和i = i + 1相同。

There is another link that says that it used to be true previously that increment operator was faster than addition and assignment, but now compilers optimize i++ and i=i+1 the same.

有任何正式文件/文件,我们可以参考,以确认究竟是什么吧? (我通常使用信用卡的和接受了计算器的人回答数去。便无法找到我提供的链接,这样的事)。

Is there any official document/paper which we can refer to confirm what is exactly right ? (I usually go with credit's and number of accepted answers of a person on stackoverflow. Could't find any such thing on the links I provided).

推荐答案

其实,如果你做C ++,它是更好的习惯来写 ++我代替。原因很简单:我++ 需要复制

Actually, if you do C++, it is much better to get used to write ++i instead. The reason is simple: i++ requires a copy.

a = ++i; // a is set to the result of i+1
a = i++; // make a copy of i, compute i+1, save the copy of i in a

如果没有优化,装配code是这样的:

Without optimizations, the assembly code would look like this:

a = ++i;                            a = i++;

MOV eax, (i)                        MOV eax, (i)
                                    PUSH eax
ADD eax, 1                          ADD eax, 1
MOV (i), eax                        MOV (i), eax
                                    POP eax
MOV (a), eax                        MOV (a), eax

现在,与优化,其结果是在C相同,其中 ++ 运营商只适用于整数和指针。

Now, with optimizations, the result is the same in C where the ++ operator only applies to integers and pointers.

++ - 在那里,因为大多数处理器有一个 INC DEC中C编写时指令。所以,如果你使用一个索引寄存器,这些指令将应用:

The ++ and -- are there because most processors had an INC and a DEC instruction at the time C was written. So if you were to use an index register, these instructions would be applied:

char a[256];
...init 'a' in some way...
int sum =0;
for(int i = 0; i < 100; ++i)
{
    sum += a[i];
}

这可与做一个简单的 INC 在(6502):

This could be done with a simple INC as in (6502):

    LDA #00
    LDY #00
LOOP:
    CLC
    ADC ($80),Y
    INY              <-- ++i or i++
    CPY #100
    BCC LOOP

请注意,在C,我们有另一个符号来增加一个变量:

Note that in C we have another notation to increment a variable:

i += 1;

这是实际的,如果你需要超过1递增寄存器:

This is practical if you need to increment the register by more than 1:

i += 3;

或者到寄存器每次加倍:

Or to double the register each time:

i += i;   // (equivalent to  i *= 2;  or  i <<= 1;  in C++)


问:为什么是 INC DEC 不能与所有的80x86使用


Question: Why is INC and DEC not used with all 80x86?

有已经时值 ADD章,1 SUB章,1 指令均高于快 INC章 DEC章。在过去,这是更快,因为指令较小,我们没有缓存(或很少)。如今,无论是指令大概是一样的。

There has been time when the ADD reg, 1 and SUB reg, 1 instructions were faster than the INC reg and DEC reg. In the old days, it was faster because the instruction was smaller and we had no cache (or very little). Today, either instruction is probably about the same.

从下面的注释,对于缓慢的一个原因是标志位寄存器:

From a comment below, a reason for the "slowness" was the FLAGS register:

<一个href=\"http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html\"相对=nofollow>英特尔优化参考,部分3.5.1.1使用INC和DEC指令的

从另一个更大的电流评论,它看起来像英特尔文档中引用已固定在新的处理器缓慢。所以在使用或不使用这些指令应取决于是否事先知道目标处理器上。

From another, more current comment, it looks like the slowness referenced in that Intel document has been fixed in newer processors. So the use or non-use of these instructions should depend on the target processor if known in advance.

正如phresnel在评论中指出,之间的差异我++ ++我可能是不明确对许多人来说。对于整数,优化真的是微不足道,肯定会用-O0甚至发生。然而,在C ++中,这是一个不同的故事。有既增加运营商充分显示出副本所需的我++类(即使数据_ 只是一个整数,但在这种情况下,你也可以这样做:返回数据_ ++ - 它仍然需要很好地隐藏副本):

As pointed out by phresnel in a comment, the difference between i++ and ++i was probably not clear for many people. For an integer, the optimization is really trivial and will most certainly happen even with -O0. However, in C++, that's a different story. There is a class with both increment operators clearly showing that a copy is required for i++ (even if data_ is just an integer, although in that case you could also do: return data_++ -- it still requires a well hidden copy!):

class A
{
public:
    A& operator ++ () // ++i -- no copy
    {
        ...apply the ++ operation to 'data_'...
        return *this;    // return a reference to this
    }

    A  operator ++ (int) // i++ -- needs a temporary copy
    {
        // remember that the 'int' is totally ignored in the function,
        // its only purpose is to distinguish '++i' from 'i++'

        A copy = *this;    // here we need a copy
        ++*this;
        return copy;       // and here we return said copy
    }

private:
    some_type_t   data_;
};

请注意,现代的C ++编译器不会在的两个副本我++ 函数返回值,而不需要额外的副本优化掉了。

Note that modern C++ compilers do not make two copies in the i++ function as the returned value can be optimized out without the need of an extra copy.

这两种情况之间的区别,如果使用我++ 如<一个可以作为清楚地显示较慢href=\"http://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c/9519095#9519095\">Is在那里,我之间,我在C ++?性能差异++和++(链接由phresnel提及)

The difference between both cases can be shown as clearly slower if using i++ as described in Is there a performance difference between i++ and ++i in C++? (link mentioned by phresnel)

这篇关于了解更多关于我++和i = i + 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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