C 中指针增量与索引增量如何比较
考虑以下两个代码:
void PrintLetter(char *src)
{
while(*src != '\0')
{
printf("%c",*src);
src++;
}
}
和
void PrintLetter(char *src)
{
int i;
for(i=0;src[i];i++)
printf("%c",src[i]);
}
两者之间有性能差异吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一点也没有。无论您编写的形式如何,编译器都会执行其优化。底层汇编代码是相同的。
None whatsoever. The compiler will perform its optimizations regardless of the form you are writing. The underlying assembly code is the same.
任何性能差异都取决于编译器。
一些小型嵌入式系统具有相当简单的编译器,可能会产生与另一个略有不同的代码 - 尽管未经测试,很难猜测哪个最终可能“更好”(尽管如果我必须“盲目”猜测,我会可能会选择第一个)。
使用典型桌面/服务器系统上的编译器(例如,gcc、VC++、EDG),无论哪种方式,您都可能获得(本质上)相同的结果,因此在它们之间进行选择纯粹是选择您认为更具可读性的问题。
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 (e.g., 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.