Code: Select all
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
function microtime_float() {
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
function benchmark_quotes($loops = 1000000) {
$times = array();
for ($i = 0; $i < 6; ++$i) {
$times[$i] = 0;
}
$name = 'timv';
$age = 25;
for ($i = 0; $i < $loops; ++$i) {
$j = 0;
$start = microtime_float();
echo sprintf('My name is %s and i\'m %d years old.<br>' . "\n", $name, $age);
$end = microtime_float();
$times[$j++] += ($end - $start);
$start = microtime_float();
echo sprintf("My name is %s and i'm %d years old.<br>\n", $name, $age);
$end = microtime_float();
$times[$j++] += ($end - $start);
$start = microtime_float();
echo 'My name is ' . $name . ' and i\'m ' . $age . ' years old.<br>' . "\n";
$end = microtime_float();
$times[$j++] += ($end - $start);
$start = microtime_float();
echo "My name is $name and i'm $age years old.<br>\n";
$end = microtime_float();
$times[$j++] += ($end - $start);
$start = microtime_float();
echo "My name is {$name} and i'm {$age} years old.<br>\n";
$end = microtime_float();
$times[$j++] += ($end - $start);
$start = microtime_float();
echo 'My name is ' , $name , ' and i\'m ' , $age , ' years old.<br>', "\n";
$end = microtime_float();
$times[$j++] += ($end - $start);
}
return $times;
}
function print_times($times) {
for ($i = 0; $i < count($times); ++$i) {
echo 'Results for ' . $i . ': ' . $times[$i] . '<br>' . "\n";
}
}
$times = array();
$runs = 5;
for ($i = 0; $i < $runs; ++$i) {
$times[] = benchmark_quotes(pow(10, $i + 1));
}
for ($i = 0; $i < $runs; ++$i) {
echo 'Results for ' . pow(10, $i + 1) . ' loops:' . "\n";
print_times($times[$i]);
echo "\n";
}
echo "Done.\n";
?>