Skip to content

Instantly share code, notes, and snippets.

@morrisonlevi
Last active December 16, 2015 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morrisonlevi/5492332 to your computer and use it in GitHub Desktop.
Save morrisonlevi/5492332 to your computer and use it in GitHub Desktop.
Empty Loop Micro-benchmarks. Useless except for curiosity.
<?php
define('ITERATIONS', 10000000);
$var = 0;
$start = microtime(true);
$var = 0;
while ($var < ITERATIONS) {
++$var;
}
$stop = microtime(true);
printf("Loop type %8s took %f seconds\n", 'while', $stop - $start);
$start = microtime(true);
$var = 0;
do {
++$var;
} while ($var < ITERATIONS);
$stop = microtime(true);
printf("Loop type %8s took %f seconds\n", 'do-while', $stop - $start);
$start = microtime(true);
for ($var = 0; $var < ITERATIONS; ++$var) {
//do nothing
}
$stop = microtime(true);
printf("Loop type %8s took %f seconds\n", 'for', $stop - $start);
@hakre
Copy link

hakre commented Apr 30, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment