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

发布于 2024-11-10 14:42:01 字数 273 浏览 7 评论 0原文

考虑以下两个代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懵少女 2024-11-17 14:42:02

一点也没有。无论您编写的形式如何,编译器都会执行其优化。底层汇编代码是相同的。

None whatsoever. The compiler will perform its optimizations regardless of the form you are writing. The underlying assembly code is the same.

╭ゆ眷念 2024-11-17 14:42:02

任何性能差异都取决于编译器。

一些小型嵌入式系统具有相当简单的编译器,可能会产生与另一个略有不同的代码 - 尽管未经测试,很难猜测哪个最终可能“更好”(尽管如果我必须“盲目”猜测,我会可能会选择第一个)。

使用典型桌面/服务器系统上的编译器(例如,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文