Which loop is faster, while or for?

后端 未结 16 2338
我寻月下人不归
我寻月下人不归 2020-11-27 07:07

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

While:

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


        
16条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 07:31

    As others have said, any compiler worth its salt will generate practically identical code. Any difference in performance is negligible - you are micro-optimizing.

    The real question is, what is more readable? And that's the for loop (at least IMHO).

提交回复
热议问题