Created
September 9, 2016 21:58
-
-
Save Perturbatio/adfe10304f65fb7bdd1adda0435c2f93 to your computer and use it in GitHub Desktop.
Using byte based comparison for startsWith and endsWith since mb_str functions are slow and it doesn't matter what the encoding is in this instance.
another performance hit is the current use of mb_strpos since the full length of the string will be checked, resulting in increasingly slow performance as the str length increases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$testHaystack = trim(file_get_contents( 'data-large.txt' )); //~4.5MB of text | |
//$testHaystack = trim(file_get_contents( 'data-small.txt')); //~1KB of text | |
//$testHaystack = 'السلام علیکم ورحمة الله وبرکاتهöLorem ipsum dolor sit amet voluptatem?öالسلام علیکم ورحمة الله وبرکاته';//Short string test | |
$validMatch = "السلام علیکم ورحمة الله وبرکاتهöLorem"; | |
$invalidMatch = "I'm not anywhere in there"; | |
$numberOfIterations = 10000; | |
//------------------[ startsWith valid ]------------------// | |
sleep( 1 ); | |
echo "startsWith against validMatch\n"; | |
$startTime1 = microtime( true ); | |
$matches = 0; | |
for ( $i = 0; $i < $numberOfIterations; $i ++ ) { | |
if ( startsWith( $testHaystack, $validMatch ) ) { | |
$matches ++; | |
} | |
} | |
echo "Finished in ", microtime( true ) - $startTime1, " seconds, {$matches} matches found\n\n"; | |
//------------------[ startsWith invalid ]------------------// | |
sleep( 1 ); | |
echo "startsWith against invalidMatch\n"; | |
$startTime1 = microtime( true ); | |
$matches = 0; | |
for ( $i = 0; $i < $numberOfIterations; $i ++ ) { | |
if ( startsWith( $testHaystack, $invalidMatch ) ) { | |
$matches ++; | |
} | |
} | |
echo "Finished in ", microtime( true ) - $startTime1, " seconds, {$matches} matches found\n\n"; | |
//------------------[ startsWithAlt valid ]------------------// | |
sleep( 1 ); | |
echo "startsWithAlt against validMatch\n"; | |
$startTime1 = microtime( true ); | |
$matches = 0; | |
for ( $i = 0; $i < $numberOfIterations; $i ++ ) { | |
if ( startsWithAlt( $testHaystack, $validMatch ) ) { | |
$matches ++; | |
} | |
} | |
echo "Finished in ", microtime( true ) - $startTime1, " seconds, {$matches} matches found\n\n"; | |
//------------------[ startsWithAlt invalid ]------------------// | |
sleep( 1 ); | |
echo "startsWithAlt against invalidMatch\n"; | |
$startTime1 = microtime( true ); | |
$matches = 0; | |
for ( $i = 0; $i < $numberOfIterations; $i ++ ) { | |
if ( startsWithAlt( $testHaystack, $invalidMatch ) ) { | |
$matches ++; | |
} | |
} | |
echo "Finished in ", microtime( true ) - $startTime1, " seconds, {$matches} matches found\n\n"; | |
/********************************************************* | |
* Ends with | |
********************************************************/ | |
$validMatch = "voluptatem?öالسلام علیکم ورحمة الله وبرکاته"; | |
$invalidMatch = "I'm not anywhere in there"; | |
//------------------[ endsWith valid ]------------------// | |
sleep( 1 ); | |
echo "endsWith against validMatch\n"; | |
$startTime1 = microtime( true ); | |
$matches = 0; | |
for ( $i = 0; $i < $numberOfIterations; $i ++ ) { | |
if ( endsWith( $testHaystack, $validMatch ) ) { | |
$matches ++; | |
} | |
} | |
echo "Finished in ", microtime( true ) - $startTime1, " seconds, {$matches} matches found\n\n"; | |
//------------------[ endsWith invalid ]------------------// | |
sleep( 1 ); | |
echo "endsWith against invalidMatch\n"; | |
$startTime1 = microtime( true ); | |
$matches = 0; | |
for ( $i = 0; $i < $numberOfIterations; $i ++ ) { | |
if ( endsWith( $testHaystack, $invalidMatch ) ) { | |
$matches ++; | |
} | |
} | |
echo "Finished in ", microtime( true ) - $startTime1, " seconds, {$matches} matches found\n\n"; | |
//------------------[ endsWithAlt valid ]------------------// | |
sleep( 1 ); | |
echo "endsWithAlt against validMatch\n"; | |
$startTime1 = microtime( true ); | |
$matches = 0; | |
for ( $i = 0; $i < $numberOfIterations; $i ++ ) { | |
if ( endsWithAlt( $testHaystack, $validMatch ) ) { | |
$matches ++; | |
} | |
} | |
echo "Finished in ", microtime( true ) - $startTime1, " seconds, {$matches} matches found\n\n"; | |
//------------------[ endsWithAlt invalid ]------------------// | |
sleep( 1 ); | |
echo "endsWithAlt against invalidMatch\n"; | |
$startTime1 = microtime( true ); | |
$matches = 0; | |
for ( $i = 0; $i < $numberOfIterations; $i ++ ) { | |
if ( endsWithAlt( $testHaystack, $invalidMatch ) ) { | |
$matches ++; | |
} | |
} | |
echo "Finished in ", microtime( true ) - $startTime1, " seconds, {$matches} matches found\n\n"; | |
//------------------[ functions to test ]------------------// | |
/** | |
* @param $haystack | |
* @param $needles | |
* | |
* @return bool | |
*/ | |
function startsWith( $haystack, $needles ) { | |
foreach ( (array) $needles as $needle ) { | |
if ( $needle != '' && mb_strpos( $haystack, $needle ) === 0 ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* @param $haystack | |
* @param $needles | |
* | |
* @return bool | |
*/ | |
function startsWithAlt( $haystack, $needles ) { | |
foreach ( (array) $needles as $needle ) { | |
//byte based comparison since we don't need to deal with the values after comparison | |
if ( $needle != '' && substr( $haystack, 0, strlen( $needle ) ) === $needle ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* @param $haystack | |
* @param $needles | |
* | |
* @return bool | |
*/ | |
function endsWith( $haystack, $needles ) { | |
foreach ( (array) $needles as $needle ) { | |
if ( (string) $needle === mb_substr( $haystack, -mb_strlen( $needle ), null, 'UTF-8' ) ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* @param $haystack | |
* @param $needles | |
* | |
* @return bool | |
*/ | |
function endsWithAlt( $haystack, $needles ) { | |
foreach ( (array) $needles as $needle ) { | |
//byte based comparison since we don't need to deal with the values after comparison | |
if ( (string) $needle === substr( $haystack, -strlen( $needle ) ) ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment