Project

General

Profile

Bug #23104 » 15002_test_v5.php

Administrator Admin, 2010-07-05 23:40

 
<?php

$times = 200002;

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

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

function isFirstPartOfStrNew2 ($str, $partStr) {
return (boolean)($partStr !== '' && strpos($str, $partStr, 0) === 0);
}

function isFirstPartOfStrNew3 ($str, $partStr) {
if ($partStr === '') return FALSE;
return (strpos($str, $partStr, 0) === 0);
}

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', '');
$time_new2_normal = test ('isFirstPartOfStrNew2', 'the quick brown fox', 'the');
$time_new2_empty = test ('isFirstPartOfStrNew2', 'the quick brown fox', '');
$time_new3_normal = test ('isFirstPartOfStrNew3', 'the quick brown fox', 'the');
$time_new3_empty = test ('isFirstPartOfStrNew3', 'the quick brown fox', '');

echo "old plain: " . $time_old_normal;
echo "\nnew plain: " . $time_new_normal;
echo "\nnew2 plain: " . $time_new2_normal;
echo "\nnew3 plain: " . $time_new3_normal;
echo "\n----------------------------";
echo "\ngain : " . number_format((($time_old_normal/$time_new_normal)*100) - 100, 1) . ' %';
echo "\ngain2 : " . number_format((($time_old_normal/$time_new2_normal)*100) - 100, 1) . ' %';
echo "\ngain3 : " . number_format((($time_old_normal/$time_new3_normal)*100) - 100, 1) . ' %';
echo "\n";
echo "\nold empty: " . $time_old_empty;
echo "\nnew empty: " . $time_new_empty;
echo "\nnew2 empty: " . $time_new2_empty;
echo "\nnew3 empty: " . $time_new3_empty;
echo "\n----------------------------";
echo "\ngain : " . number_format((($time_old_empty/$time_new_empty)*100) - 100, 1) . ' %';
echo "\ngain2 : " . number_format((($time_old_empty/$time_new2_empty)*100) - 100, 1) . ' %';
echo "\ngain3 : " . number_format((($time_old_empty/$time_new3_empty)*100) - 100, 1) . ' %';
echo "\n";

?>
(6-6/10)