(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.000039 0.000038 0.000055 0.000044 100.00%
for loop var++ 0.000038 0.000038 0.000054 0.000043 98.19%
while loop ++var 0.000040 0.000040 0.000057 0.000046 103.79%
while loop var++ 0.000041 0.000039 0.000053 0.000044 100.72%
do-while loop ++var 0.000040 0.000040 0.000059 0.000046 105.23%
do-while loop var++ 0.000041 0.000052 0.000055 0.000049 111.91%
goto ++var 0.000041 0.000063 0.000052 0.000052 118.05%
goto var++ 0.000041 0.000068 0.000040 0.000050 113.00%

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;
    }
}