|
<?php
|
|
|
|
$times = 30000;
|
|
|
|
function isFirstPartOfStr ($str, $partStr) {
|
|
$psLen = strlen($partStr);
|
|
if ($psLen) {
|
|
return substr($str, 0, $psLen) == (string)$partStr;
|
|
} else return FALSE;
|
|
}
|
|
|
|
function isFirstPartOfStrVariant1 ($str, $partStr) {
|
|
if ($partStr === '') return FALSE;
|
|
if (strpos($str, $partStr, 0) === 0) {
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
function isFirstPartOfStrVariant2 ($str, $partStr) {
|
|
return (boolean)($partStr !== '' && strpos($str, $partStr, 0) === 0);
|
|
}
|
|
|
|
function isFirstPartOfStrVariant3 ($str, $partStr) {
|
|
if ($partStr === '') return FALSE;
|
|
return (strpos($str, $partStr, 0) === 0);
|
|
}
|
|
|
|
function isFirstPartOfStrVariant4 ($str, $partStr) {
|
|
if ((string)$partStr === '') return FALSE;
|
|
return (strpos((string)$str, (string)$partStr, 0) === 0);
|
|
}
|
|
|
|
function isFirstPartOfStrVariant5 ($str, $partStr) {
|
|
if (!is_string($string) || !is_string($partOfString) || $string === '' || $partOfString === '') {
|
|
return FALSE;
|
|
}
|
|
if (strpos($string, $partOfString, 0) === 0) {
|
|
return TRUE;
|
|
}
|
|
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);
|
|
}
|
|
|
|
$testcases = array (
|
|
'normal usage two strings' => array (
|
|
'the quick brown fox',
|
|
'the'
|
|
),
|
|
'empty str' => array (
|
|
'',
|
|
'the quick brown fox'
|
|
),
|
|
'empty partStr' => array (
|
|
'the quick brown fox',
|
|
''
|
|
),
|
|
'null str' => array (
|
|
NULL,
|
|
'the quick brown fox'
|
|
),
|
|
'null partStr' => array (
|
|
'the quick brown fox',
|
|
NULL
|
|
),
|
|
'array str' => array (
|
|
array ('a', 'b'),
|
|
'the quick brown fox'
|
|
),
|
|
'array partStr' => array (
|
|
'the quick brown fox',
|
|
array ('a', 'b')
|
|
),
|
|
);
|
|
|
|
foreach ($testcases as $name => $parameters) {
|
|
echo "\n$name";
|
|
|
|
$baseTime = test ('isFirstPartOfStr', $parameters[0], $parameters[1]);
|
|
echo "\noriginal : " . sprintf("%.3f", $baseTime);
|
|
$time = test ('isFirstPartOfStrVariant1', $parameters[0], $parameters[1]);
|
|
echo "\nvariant1 : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';
|
|
$time = test ('isFirstPartOfStrVariant2', $parameters[0], $parameters[1]);
|
|
echo "\nvariant2 : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';
|
|
$time = test ('isFirstPartOfStrVariant3', $parameters[0], $parameters[1]);
|
|
echo "\nvariant3 : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';
|
|
$time = test ('isFirstPartOfStrVariant4', $parameters[0], $parameters[1]);
|
|
echo "\nvariant4 : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';
|
|
$time = test ('isFirstPartOfStrVariant5', $parameters[0], $parameters[1]);
|
|
echo "\nvariant5 : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';
|
|
|
|
echo "\n";
|
|
}
|
|
|
|
?>
|