简体   繁体   English

指针增量与 C 中的索引增量相比如何

[英]How does pointer increment compare with index increment in C

consider the following two code:考虑以下两个代码:

void PrintLetter(char *src)
{
 while(*src != '\0')
 {
   printf("%c",*src);
   src++;
 }
}

and

void PrintLetter(char *src)
{
 int i;
 for(i=0;src[i];i++)
  printf("%c",src[i]);
}

Is there any performance difference between the two?两者之间有性能差异吗?

None whatsoever.没有任何。 The compiler will perform its optimizations regardless of the form you are writing.无论您编写何种形式,编译器都会执行其优化。 The underlying assembly code is the same.底层汇编代码是相同的。

Any performance difference will depend on the compiler.任何性能差异都取决于编译器。

Some small embedded systems have quite simplistic compilers that may produce slightly different code for one than the other -- though without testing, it's hard to guess which might end up "better" (though if I had to guess "blindly", I'd probably pick the first).一些小型嵌入式系统具有非常简单的编译器,可能会为一个编译器生成与另一个稍有不同的代码——尽管没有测试,很难猜测哪个最终会“更好”(尽管如果我不得不“盲目地”猜测,我会可能会选择第一个)。

With the compilers on typical desktop/server systems (eg, gcc, VC++, EDG) you're likely to get (essentially) identical results either way, so choosing between them is purely a matter of picking what you find more readable.使用典型桌面/服务器系统(例如,gcc、VC++、EDG)上的编译器,无论哪种方式,您都可能获得(基本上)相同的结果,因此在它们之间进行选择纯粹是选择您认为更具可读性的内容。

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

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