(goto list of all tests)

Empty loops

This is just a simple test of 3 different loops (for, while and do-while).

Link to the blog post about this test.

We're running the 3 different loops through with both post and pre increment.
Running them all 4 times, once with a single iteration, then with 5000, 5001 and 4999 iterations.

Title500050014999AvarageDifference
for loop ++var 0.000133 0.000134 0.000133 0.000133 100.00%
for loop var++ 0.000161 0.000170 0.000221 0.000184 138.04%
while loop ++var 0.000140 0.000131 0.000127 0.000133 99.52%
while loop var++ 0.000126 0.000125 0.000126 0.000126 94.28%
do-while loop ++var 0.000115 0.000115 0.000115 0.000115 86.23%
do-while loop var++ 0.000111 0.000111 0.000117 0.000113 84.79%
goto ++var 0.000127 0.000133 0.000126 0.000129 96.54%
goto var++ 0.000124 0.000124 0.000124 0.000124 93.08%

The do-while and while loops seems to be faster than the for loops.

Source:

<?php
// --- Informative block
$title          "Empty loops";
$teaser         "This is just a simple test of 3 different loops (<em>for</em>, <em>while</em> and <em>do-while</em>).";
$description    "We're running the 3 different loops through with both post and pre increment.<br />Running them all 4 times, once with a single iteration, then with 5000, 5001 and 4999 iterations.";
$conclusion     "The do-while and while loops seems to be faster than the for loops.";
$link            "http://birk-jensen.dk/2010/11/benchmark-class-release-first-test/";

// --- Actual test
if ( isset($runTest) ) {
    
// -- Initialize the test
    
require_once(dirname(__FILE__) . "/../Bench.php");
    
$b = new Bench();
    
    
$it = array(1500050014999);
    
$dontSave true;
    
    
$headers = array("5000""5001""4999");
    
    foreach (
$it as $itValue) {
        
// For loop ++$i
        
$b->start("for loop ++var"$itValue$dontSave);
        for (
$i 0$i $itValue; ++$i) {
            
// Nothing
        
}
        
$b->end("for loop ++var"$itValue);
    
        
// For loop $i++:
        
$b->start("for loop var++"$itValue$dontSave);
        for (
$i 0$i $itValue$i++) {
            
// Nothing
        
}
        
$b->end("for loop var++"$itValue);
        
        
// while ++$i
        
$b->start("while loop ++var"$itValue$dontSave);
        
$i 0;
        while ( ++
$i $itValue) {
            
// Nothing
        
}
        
$b->end("while loop ++var"$itValue);
        
        
// While loop $i++:
        
$b->start("while loop var++"$itValue$dontSave);
        
$i 0;
        while ( 
$i++ < $itValue) {
            
// Nothing
        
}
        
$b->end("while loop var++"$itValue);
        
        
// Do while ++$i:
        
$b->start("do-while loop ++var"$itValue$dontSave);
        
$i 0;
        do {
            
// Nothing
        
} while (++$i $itValue);
        
$b->end("do-while loop ++var"$itValue);
        
        
// Do while $i++
        
$b->start("do-while loop var++"$itValue$dontSave);
        
$i 0;
        do {
            
// Nothing
        
} while ($i++ < $itValue);
        
$b->end("do-while loop var++"$itValue);
        
        
$b->start("goto ++var"$itValue$dontSave);
        
$i 0;
        
a:
        if (++
$i $itValue) {
            goto 
a;
        }
        
$b->end("goto ++var"$itValue);
        
$b->start("goto var++"$itValue$dontSave);
        
$i 0;
        
b:
        if (
$i++ < $itValue) {
            goto 
b;
        }
        
$b->end("goto var++"$itValue);
        
        
$dontSave false;
    }
}