哪个循环更快,while 还是 for? [英] Which loop is faster, while or for?

查看:68
本文介绍了哪个循环更快,while 还是 for?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以使用 for 和 while 循环获得相同的输出:

You can get the same output with for and while loops:

虽然:

$i = 0;
while ($i <= 10){
  print $i."
";
  $i++;
};

对于:

for ($i = 0; $i <= 10; $i++){
  print $i."
";
}

但哪个更快?

推荐答案

这显然取决于特定语言的解释器/编译器的特定实现.

That clearly depends on the particular implementation of the interpreter/compiler of the specific language.

也就是说,从理论上讲,如果速度更快,任何理智的实现都可能能够在另一个方面实现一个,因此差异最多可以忽略不计.

That said, theoretically, any sane implementation is likely to be able to implement one in terms of the other if it was faster so the difference should be negligible at most.

当然,我假设 whilefor 的行为与它们在 C 和类似语言中的行为一样.你可以为 whilefor

Of course, I assumed while and for behave as they do in C and similar languages. You could create a language with completely different semantics for while and for

这篇关于哪个循环更快,while 还是 for?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆