- microtime: documentation ( source)
- str_replace: documentation ( source)
- preg_replace: documentation ( source)
<?php
$startTimeOne = microtime(true);
for ($i = 0; $i <= 10000; $i++) {
str_replace('-', '+', 'my-name-is-am-and-i-like-trains');
}
$endTimeOne = microtime(true);
for ($j = 0; $j <= 10000; $j++) {
str_replace('-', '+', 'foo-bar');
}
$endTimeTwo = microtime(true);
for ($j = 0; $j <= 10000; $j++) {
preg_replace('#-#', '+', 'my-name-is-am-and-i-like-trains');
}
$endTimeThree = microtime(true);
for ($j = 0; $j <= 10000; $j++) {
preg_replace('#-#', '+', 'foo-bar');
}
$endTimeFour = microtime(true);
echo ($endTimeOne - $startTimeOne) * 1000 . "\n";
echo ($endTimeTwo - $endTimeOne) * 1000 . "\n";
echo ($endTimeThree - $endTimeTwo) * 1000 . "\n";
echo ($endTimeFour - $endTimeThree) * 1000;