(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.000096 0.000093 0.000087 0.000092 100.00%
for loop var++ 0.000120 0.000107 0.000109 0.000112 121.87%
while loop ++var 0.000103 0.000134 0.000086 0.000108 117.03%
while loop var++ 0.000199 0.000089 0.000082 0.000123 134.05%
do-while loop ++var 0.000072 0.000095 0.000071 0.000079 86.26%
do-while loop var++ 0.000069 0.000070 0.000101 0.000080 87.12%
goto ++var 0.000086 0.000103 0.000107 0.000099 107.35%
goto var++ 0.000102 0.000082 0.000098 0.000094 102.25%

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