3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('ITERATIONS', 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";

preferences:
30.03 ms | 402 KiB | 5 Q