PHP Do-While Versus the While Loop
May 21st, 2008 - GorkfuWell today I happened to be looking at some very old php code. It was done in PHP4, but that really doesn’t matter. Anyways, when I was going through the code I noticed the person who had written the code did lots of do-while loops. I don’t do any “do-while” loops mainly out of personal preference. I do “regular while” loops mainly for the reason that they are more practical to me. More will be explained below about this. However, it got me thinking today and I had to ask myself this question; Which loop would load faster?
The Difference Between a Do-While and a While Loop
First I’m going to take the time to talk about the difference between a do while and a while loop before getting into the test. Basically a do-while and a while loop work relatively the same except for one condition. A do-while loop checks the test condition (the condition that is defined inside the while) at the end of the loop. Regular while loops check the test condition at the beginning of the loop. This means that essentially do-while loops perform one extra loop over a regular while loop. Check the example below for a simple explanation of how these are laid out.
Examples:
Do-While Loop
do { this }; while {this happens};
Regular While Loop
while {this happens} { // Do This }
Testing the Loops
Using the following two slices of code we will test which loop performs faster and more efficiently. (Yes I said slices! The term snippet is used far too much these days.)
// Our Do-While Loop
do
{
$counter++;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
}while($counter < 9000000);
// Our Regular While Loop
while($counter < 9000000) {
$counter++;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
$z = $z + 5;
}
Yes your eyes don’t deceive you, I repeatedly used the same meaningless equation ten times in a row and yes I set the loop to loop nine million times. If you try this exactly as I did and load your page in FireFox 2.0, you’ll most likely freeze FireFox. It made a good test for my browsers, Opera 9.27 never froze on me till I tried to do a loop a trillion times.
I made my test with the following code. Please note the following code only works in PHP5. You will see incorrect results in PHP4.
< ?php
$start = microtime(TRUE); //Start Time Execution
$counter = 1; // Counter for uhh counting
//Loop Code Goes Here
$page_time = round(microtime(TRUE) - $start, 3) + ‘0.02′; // Get the time it took for page load
echo $page_time; // Displays the time it took to load the page
?>
Now for the results.
Regular Loop Results
- Result 1 = 8.555
- Result 2 = 8.649
- Result 3 = 8.594
- Result 4 = 8.02
- Result 5 = 8.21
- Result 6 = 8.087
- Result 7 = 8.184
- Result 8 = 8.347
- Result 9 = 8.056
- Result 10 = 8.065
Average = 8.27 (Rounded Off)
Do-While Loop Results
- Result 1 = 8.029
- Result 2 = 7.885
- Result 3 = 8.023
- Result 4 = 8.238
- Result 5 = 8.255
- Result 6 = 8.069
- Result 7 = 8.192
- Result 8 = 7.928
- Result 9 = 7.942
- Result 10 = 8.16
Average = 8.072 (Rounded Off)
Well if you do this test or something similar, the results that you get will have a slight impact from the server you test from. If it’s a local server or a remote server the times you get will always be different. Usually the times will be faster on a local server but this is not always the case. There can always be other things that affect the results such as processes or memory.
So you ask, “What do these results even mean? They look almost exactly the same”. As I expected the test shows that regular while loops perform a little faster than do-while loops on average. The test just proves that there is only a slight noticeable performance level difference between using a do while and a regular while loop. Although the amount this test did, is not completely accurate. We would need bigger strenuous tests to get better results.
Well, I hope you didn’t think that this blog post wasn’t pointless. Hopefully you may have learned something new. Maybe you now know the difference between a do-while and a regular while loop. Or maybe you learned how to create and display page generation times in PHP5. No matter the outcome, I hope you learned something. Now on to the next post!
Digg buttons brought to you by diggZ-ET (WordPress Plugin)
Tags: PHP, PHP Coding, PHP Loops