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

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

问题描述



while:

  $ i = 0; 
while($ i <= 10){
print $ i。\\\
;
$ i ++;
};



  for($ i = 0; $ i <= 10; $ i ++){
print $ i。\\\
;
}

但是哪一个更快?



也就是说,从理论上讲,如果速度更快,那么任何理智的实现都可能能够实现一个,所以差异最多可以忽略不计。

当然,我假设 c>像在C语言和类似语言中一样行事。你可以为创建一个语言完全不同的语义,而


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

While:

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

For:

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

But which one is faster?

解决方案

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.

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

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

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