简体   繁体   English

为什么 Visual Studio 将 i++ 后增量作为自动完成 for 循环的默认值?

[英]why does visual studio have i++ post increment as a default for autocompleting for loop?

so this is what visual studio gives when auto completing a for loop.所以这就是 Visual Studio 在自动完成 for 循环时给出的。 I have been told that ++i is more effecient than i++ as the pre increment does not require the comiler to create a temporary variable to store i.我被告知 ++i 比 i++ 更有效,因为预增量不需要编译器创建一个临时变量来存储 i。 and that I should use ++i unless I actually require i++.并且我应该使用 ++i,除非我真的需要 i++。 is there any reason that visual studio does this as a default or am I just overthinking it?视觉工作室有什么理由默认这样做还是我只是想多了?

for (size_t i = 0; i < length; i++)
{

}

Some of this answer is based on my experience rather than on any hard facts, so please take it with a grain of salt.这个答案的一部分是基于我的经验,而不是任何确凿的事实,所以请持保留态度。

Reasons for i++ as the default loop incrementor i++ 作为默认循环增量器的原因

Postfix x++ produces a temporary that's passed 'upwards' in the expression, while the the variable x is subsequently incremented.后缀 x++ 产生一个在表达式中“向上”传递的临时变量,而变量 x 随后递增。

Prefix ++x does not produce a temporary object, but increments 'x' and passes the result to the expression.前缀 ++x 不会产生临时的 object,而是递增“x”并将结果传递给表达式。

Performance表现

Looking at two simple loops, one using i++, and the other ++i, let us examine the generated assembly查看两个简单的循环,一个使用 i++,另一个使用 ++i,让我们检查生成的程序集

#include <stdio.h>

int main()
{ 
    for (int i = 0; i < 5; i++) {
     //        
   }

   for (int j = 0; j < 5; ++j) {
       //
   }


    return 0;
}

Generates the following assembly using GCC:使用 GCC 生成以下程序集:

main:
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-4], 0
.L3:
        cmp     DWORD PTR [rbp-4], 4
        jg      .L2
        add     DWORD PTR [rbp-4], 1
        jmp     .L3
.L2:
        mov     DWORD PTR [rbp-8], 0
.L5:
        cmp     DWORD PTR [rbp-8], 4
        jg      .L4
        add     DWORD PTR [rbp-8], 1
        jmp     .L5
.L4:
        mov     eax, 0
        pop     rbp
        ret

.L3 corresponds to the first loop, and.L5 corresponds to the second loop. .L3 对应于第一个循环,.L5 对应于第二个循环。 The compiler optimizes away any differences in this case resulting in identical code, so no performance difference.在这种情况下,编译器会优化掉任何差异,从而产生相同的代码,因此没有性能差异。

Appearance外貌

Looking at the following three loops, which one appeals to you most aesthetically?看看以下三个循环,哪一个在美学上最吸引你?

   for (int i = 0; i < 5; i++) {
     //        
   }

   for (int i = 0; i < 5; ++i) {
       //
   }

   for (int i = 0; i < 5; i+=1) {
       //
   }

For me i+=1 is right out, and ++i just feels sort of... backwards对我来说 i+=1 是正确的,而 ++i 只是感觉有点……倒退

Ergonomics人体工程学

  • i++) - Your hand is already in the correct position to type ) after typing ++ i++) - 输入 ++ 后,您的手已经在正确的 position 中输入 )
  • ++i) - Requires moving from upper row to homerow and back ++i) - 需要从上排移动到本垒并返回

History历史

Older C programmers, and current C programmers who write systems level code make frequent use of i++ to allow for writing very compact code such as:编写系统级代码的旧 C 程序员和当前 C 程序员经常使用 i++ 以允许编写非常紧凑的代码,例如:

// print some char * s
while(*s)
    putc(*s++);

// strcpy from src to dest
while (*dest++=*src++);

Because of this i++ became the incrementor C programmers reach for first, eventually becoming a sort of standard in cases where ++i vs i++ have the same functionally.因此,i++ 成为了程序员首先追求的增量器 C,最终在 ++i 与 i++ 具有相同功能的情况下成为一种标准。

Choosing ++i instead there would merely be a style choice .选择++i只会有一个样式选择

There are no performance gains to be had in this context .在这种情况下没有性能提升 That is a myth.那是一个神话。

i++ is also more "conventional" for a simple loop over int s.对于int s 上的简单循环, i++也更“传统”。

You are of course free to write your own loop in your own style!您当然可以自由地以自己的风格编写自己的循环!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM