Well today I happened to be looking at some very old PHP code. It was done in PHP 4, but that really doesn’t matter. Anyways, when I was going through the code I noticed that the original coder had written dozens and dozens 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 feel more practical to me. More will be explained below regarding 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

Firstly, I’m going to take the time to talk about the difference between a do while and a while loop before getting into the nitty gritty 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 examples below for an 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

With the following two slices of code we will test and find out which type of 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 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. It’s interesting thing, but off topic and maybe something to talk about another day.

My test was set up with the following code:

$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 the page to load

echo $page_time; // Display the total time it took to load the page

Now for the results.

Regular While 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)

Conclusion

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”. I didn’t expect the test to show do-while loops performing a tad faster than regular 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 this blog post was pointless; Hopefully, you may have learned something new in the process. 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 PHP. No matter the outcome, I hope you learned something.

  1. Very interesting post.
    I had a small doubt in my mind since the do while loop was not being used so often in our latest codes. Including all that we code as well as the code we can see in latest libraries/frameworks.

    This is indeed a great eye opener.
    But on a personal opinion. I also stick to the authors notion that while loop looks a lot more sane while coding normal loops than for a do while loop.
    Thanks
    tomy

  2. I doubth Firefox actually froze, it was probably displaying a “waiting for server” message some where. As PHP is executed on the server and you are not outputting anything it’s no grater a browser test then:

    All that happens is that the server is taking a long time to respond.

  3. You need to do a bit of stats on the results (specifically a two-tailed Student T-test), to see if the averages are significantly different — given that both sets of results show variation, you’d expect the averages to be different, and therefore one of the two will always seem to be the ‘winner’.
    In fact, the p value of your test is 0.031 — that is, you’d expect this result (by chance alone) three times every hundred times you repeat your test, even if you were testing like with like (i.e. comparing ‘do while’ with ‘do while’, or ‘while’ with ‘while’).
    That’s what a statistician would call a ‘marginally significant’ result — rarer than five time in a hundred (commonly taken as the cut-off point for *any* significance in the result), but not as rare as one time in a hundred (commonly taken as the ‘cast-iron’ confirmation of significance) .
    Good work, though — I wouldn’t have come across your page if I hadn’t been wondering about this exact issue!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>