Project

General

Profile

Bug #23104 » 15002_test.php

Administrator Admin, 2010-07-05 09:38

 
<?php

$times = 5000;

function isFirstPartOfStrNew ($str, $partStr) {
if ($partStr === '') return false;
if (strpos($str, $partStr, 0) === 0) {
return true;
}
return false;
}

function isFirstPartOfStrOld ($str, $partStr) {
$psLen = strlen($partStr);
if ($psLen) {
return substr($str, 0, $psLen) == (string)$partStr;
} else return false;
}

function test ($function, $a, $b) {
global $times;
$time_start = microtime(true);
for ($i = 0; $i < $times; $i++) {
$function($a, $b);
}
return (microtime(true) - $time_start);
}

$time_old_normal = test ('isFirstPartOfStrOld', 'the quick brown fox', 'the');
$time_old_empty = test ('isFirstPartOfStrOld', 'the quick brown fox', '');
$time_new_normal = test ('isFirstPartOfStrNew', 'the quick brown fox', 'the');
$time_new_empty = test ('isFirstPartOfStrNew', 'the quick brown fox', '');

echo "old plain: " . $time_old_normal;
echo "\nnew plain: " . $time_new_normal;
echo "\n----------------------------";
echo "\ngain : " . number_format((($time_old_normal/$time_new_normal)*100) - 100, 1) . ' %';
echo "\n";
echo "\nold empty: " . $time_old_empty;
echo "\nnew empty: " . $time_new_empty;
echo "\n----------------------------";
echo "\ngain : " . number_format((($time_old_empty/$time_new_empty)*100) - 100, 1) . ' %';
echo "\n";

?>
(2-2/10)