Skip to content

Instantly share code, notes, and snippets.

@kibao
Last active August 29, 2015 14:01
Show Gist options
  • Save kibao/a25de6772ed0b6de12e8 to your computer and use it in GitHub Desktop.
Save kibao/a25de6772ed0b6de12e8 to your computer and use it in GitHub Desktop.
Compare explode with in_array vs mb_strpos
<?php
define('ITERATIONS', 1000 * 1000);
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function keywordsWithoutMatch()
{
return "value1|value2|cat|dog|Plan du scénario|Plan du Scénario";
}
function keywordsWithMatch()
{
return "test|Lëtzebuergesch|Forgatókönyv|Pirate";
}
$native = "Pirate";
function benchmarkInArray($native)
{
for ($i = 0; $i < ITERATIONS; $i++) {
$keywordTypes = array(
'Given' => explode('|', keywordsWithoutMatch()),
'When' => explode('|', keywordsWithoutMatch()),
'Then' => explode('|', keywordsWithoutMatch()),
'And' => explode('|', keywordsWithoutMatch()),
'But' => explode('|', keywordsWithMatch())
);
foreach ($keywordTypes as $type => $keywords) {
if (in_array($native, $keywords) || in_array($native . '<', $keywords)) {
if ($type != 'But') throw new Exception();
break;
}
}
}
}
function benchmarkInArrayWithCache($native)
{
$keywordTypes = array(
'Given' => explode('|', keywordsWithoutMatch()),
'When' => explode('|', keywordsWithoutMatch()),
'Then' => explode('|', keywordsWithoutMatch()),
'And' => explode('|', keywordsWithoutMatch()),
'But' => explode('|', keywordsWithMatch())
);
for ($i = 0; $i < ITERATIONS; $i++) {
foreach ($keywordTypes as $type => $keywords) {
if (in_array($native, $keywords) || in_array($native . '<', $keywords)) {
if ($type != 'But') throw new Exception();
break;
}
}
}
}
function benchmarkMbStrpos($native)
{
for ($i = 0; $i < ITERATIONS; $i++) {
$keywordTypes = array(
'Given' => keywordsWithoutMatch(),
'When' => keywordsWithoutMatch(),
'Then' => keywordsWithoutMatch(),
'And' => keywordsWithoutMatch(),
'But' => keywordsWithMatch()
);
foreach ($keywordTypes as $type => $keywords) {
if (false !== mb_strpos($keywords, $native)) {
if ($type != 'But') throw new Exception();
break;
}
}
}
}
function benchmarkMbStrposWithCache($native)
{
$keywordTypes = array(
'Given' => keywordsWithoutMatch(),
'When' => keywordsWithoutMatch(),
'Then' => keywordsWithoutMatch(),
'And' => keywordsWithoutMatch(),
'But' => keywordsWithMatch()
);
for ($i = 0; $i < ITERATIONS; $i++) {
foreach ($keywordTypes as $type => $keywords) {
if (false !== mb_strpos($keywords, $native)) {
if ($type != 'But') throw new Exception();
break;
}
}
}
}
$time_start = microtime_float();
benchmarkInArray($native);
echo "benchmark in_array: " . (microtime_float() - $time_start) . "\n";
$time_start = microtime_float();
benchmarkInArrayWithCache($native);
echo "benchmark in_array with cache: " . (microtime_float() - $time_start) . "\n";
$time_start = microtime_float();
benchmarkMbStrpos($native);
echo "benchmark mb_strpos: " . (microtime_float() - $time_start) . "\n";
$time_start = microtime_float();
benchmarkMbStrposWithCache($native);
echo "benchmark mb_strpos with cache: " . (microtime_float() - $time_start) . "\n";
// PHP 5.4.24 (cli)
benchmark in_array: 8.2751109600067
benchmark in_array with cache: 3.9663600921631
benchmark mb_strpos: 12.01834821701
benchmark mb_strpos with cache: 10.596436023712
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment