Which loop is faster, while or for?

后端 未结 16 2334
我寻月下人不归
我寻月下人不归 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条回答
  •  臣服心动
    2020-11-27 07:16

    Set the loop iterations to 10,000.

    Find the time in milliseconds>Run Loop>find time in milliseconds and subtract the first timer.

    Do it for both codes, what ever one has the lowest milliseconds it runs faster. You might want to run the test multiple times and average them out to reduce the likelihood of background processes influencing the test.

    You are likely to get really similar times on both of them, but I am interested to see if one is always just slightly faster.

提交回复
热议问题