Project

General

Profile

Bug #23104 » 15002_test_v3.php

Administrator Admin, 2010-07-05 17:41

 
<?php

$times = 50000;

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

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

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

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', '');
$time_new_terse_normal = test ('isFirstPartOfStrNewTerse', 'the quick brown fox', 'the');
$time_new_terse_empty = test ('isFirstPartOfStrNewTerse', 'the quick brown fox', '');
$time_new_singlereturn_normal = test ('isFirstPartOfStrNewSingleReturn', 'the quick brown fox', 'the');
$time_new_singlereturnshort_normal = test ('isFirstPartOfStrNewSingleReturnShort', 'the quick brown fox', 'the');
$time_new_singlereturn_empty = test ('isFirstPartOfStrNewSingleReturn', 'the quick brown fox', '');
$time_new_singlereturnshort_empty = test ('isFirstPartOfStrNewSingleReturnShort', 'the quick brown fox', '');

echo "old plain : " . $time_old_normal;
echo "\nnew plain : " . $time_new_normal . ' gain: ' . number_format((($time_old_normal/$time_new_normal)*100) - 100, 1) . ' %';
echo "\nnew terse : " . $time_new_terse_normal . ' gain: ' . number_format((($time_old_normal/$time_new_terse_normal)*100) - 100, 1) . ' %';
echo "\nnew single return: " . $time_new_singlereturn_normal . ' gain: ' . number_format((($time_old_normal/$time_new_singlereturn_normal)*100) - 100, 1) . ' %';
echo "\nnew single short : " . $time_new_singlereturnshort_normal . ' gain: ' . number_format((($time_old_normal/$time_new_singlereturnshort_normal)*100) - 100, 1) . ' %';
;
echo "\n";
echo "\nold empty : " . $time_old_empty;
echo "\nnew empty : " . $time_new_empty . "gain : " . number_format((($time_old_empty/$time_new_empty)*100) - 100, 1) . ' %';;
echo "\nnew terse : " . $time_new_terse_empty . "gain : " . number_format((($time_old_empty/$time_new_terse_empty)*100) - 100, 1) . ' %';;
echo "\nnew single return: " . $time_new_singlereturn_empty . "gain : " . number_format((($time_old_empty/$time_new_singlereturn_empty)*100) - 100, 1) . ' %';;
;
echo "\nnew single short : " . $time_new_singlereturnshort_empty . "gain : " . number_format((($time_old_empty/$time_new_singlereturnshort_empty)*100) - 100, 1) . ' %';;
;

?>
(5-5/10)