简体   繁体   English

分配值时,char *指针上的C ++预增量与后增量

[英]C++ pre-increment vs post-increment on char* pointer while assigning a value

so I get it that pre-increment is faster than post-increment as no copy of the value is made. 所以我知道,因为没有复制值,所以预增量比后增量快。 But let's say I have this: 但是,假设我有这个:

char * temp = "abc"; 
char c = 0; 

Now if I want to assign 'a' to c and increment temp so that it now points to 'b' I would do it like this : 现在,如果我想将“ a”分配给c并增加temp ,使其现在指向“ b”,我将这样做:

c = *temp++; 

but pre-increment should be faster so i thought : 但预增量应该更快,所以我认为:

c = *temp; 
++temp;

but it turns out *temp++ is faster according to my measurements. 但是事实证明,根据我的测量,* temp ++更快。

Now I don't quite get it why and how, so if someone is willing to enlighten me, please do. 现在我还不太清楚为什么以及为什么,所以如果有人愿意启发我,请这样做。

First, pre-increment is only potentially faster for the reason you state. 首先,仅出于您说明的原因,预增量可能会更快。 But for basic types like pointers, in practice that's not the case, because the compiler can generate optimized code. 但是对于诸如指针之类的基本类型,实际上并非如此,因为编译器可以生成优化的代码。 Ref. 参考。 Is there a performance difference between i++ and ++i in C++? C ++中的i ++和++ i在性能上有区别吗? for more details. 更多细节。

Second, to a decent optimizing compiler, there's no difference between the two alternatives you mentioned. 其次,对于像样的优化编译器,您提到的两种选择之间没有区别。 It's very likely the same exact machine code will be generated for both cases (unless you disabled optimizations). 两种情况下很可能会生成完全相同的机器代码(除非您禁用了优化)。

To illustrate this : on my compiler, the following code is generated when optimizations are disabled : 为了说明这一点:在我的编译器上,禁用优化时会生成以下代码:

# c = *temp++;
movq    temp(%rip), %rax
leaq    1(%rax), %rdx
movq    %rdx, temp(%rip)
movzbl  (%rax), %eax
movb    %al, c(%rip)

# c = *temp;
movq    temp(%rip), %rax
movzbl  (%rax), %eax
movb    %al, c(%rip)
# ++temp;
movq    temp(%rip), %rax
addq    $1, %rax
movq    %rax, temp(%rip)

Notice there's an additional movq instruction in the latter, which could account for slower run time. 请注意,后者中还有一条movq指令,可能会导致运行时间变慢。

When enabling optimizations however, this turns into : 但是,当启用优化时,将变成:

# c = *temp++;
movq    temp(%rip), %rax
leaq    1(%rax), %rdx
movq    %rdx, temp(%rip)
movzbl  (%rax), %eax
movb    %al, c(%rip)

# c = *temp;
# ++temp;
movq    temp(%rip), %rax
movzbl  (%rax), %edx
addq    $1, %rax
movq    %rax, temp(%rip)
movb    %dl, c(%rip)

Other than a different order of the instructions, and the choice of using addq vs. leaq for the increment, there's no real difference between these two. 除了指令的不同顺序,以及选择使用addq leaq进行增量选择之外,这两者之间没有真正的区别。 If you do get (measurably) different performance between these two, then that's likely due to the specific cpu architecture (possibly a more optimal use of the pipeline eg.). 如果您确实在这两者之间获得了(明显)不同的性能,则可能是由于特定的cpu架构(例如,可能是对管道的更优化使用)造成的。

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

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