- microtime: documentation ( source)
- printf: documentation ( source)
<?php
$context = array('test' => array());
// optionally fill-in the test value with lots of data
for ($i = 0; $i < 100000; $i++) {
$context['test'][$i] = $i;
}
// you can also just create a big string
// $context = str_repeat(' ', 1000000);
// benchmark
$time = microtime(true);
for ($i = 0; $i < 100; $i++) {
// the snippet of code to benchmark
$tmp = isset($context['test']) ? $context['test'] : '';
}
printf("TERNARY: %0.2d\n", (microtime(true) - $time) * 1000) . PHP_EOL;
// benchmark
$time = microtime(true);
for ($i = 0; $i < 100; $i++) {
// the snippet of code to benchmark
$tmp = '';
if (isset($context['test'])) {
$tmp = $context['test'];
}
}
printf("IF : %0.2d\n", (microtime(true) - $time) * 1000);
// benchmark
$time = microtime(true);
for ($i = 0; $i < 100; $i++) {
// the snippet of code to benchmark
if (isset($context['test'])) {
$tmp = $context['test'];
} else {
$tmp = '';
}
}
printf("IF/ELSE: %0.2d\n", (microtime(true) - $time) * 1000);