[php-src] Issue #7847: stripos has very bad performance

From: Date: Fri, 31 Dec 2021 16:54:15 +0000
Subject: [php-src] Issue #7847: stripos has very bad performance
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-238685@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/7847 Comment Author: iluuu1994 ```php function test($haystack, $needle) { $repetitions = 0; $start = microtime(true); do { stripos($haystack, $needle); $repetitions += 1; } while (microtime(true) - $start < 5); $rate = round($repetitions / (microtime(true) - $start)); $rate = str_pad($rate, 8, ' ', STR_PAD_LEFT); echo "$rate / second\n"; } // Worst case test(str_repeat('A', 1e+7) . 'BB', 'bb'); test(str_repeat('aA', 1e+6) . 'aB', 'ab'); test(str_repeat('A', 1e+1) . 'BB', 'bb'); test(str_repeat('aA', 1e+1) . 'aB', 'ab'); // Best case test(str_repeat('A', 1e+7), 'aa'); test(str_repeat('aA', 1e+6), 'aa'); test(str_repeat('A', 1e+1), 'aa'); test(str_repeat('aA', 1e+1), 'aa'); // Real life cases test(file_get_contents(__DIR__ . '/php_net.html'), '<article'); // Content of https://www.php.net/ test('content-type: application/json', 'content-type:'); test('int', 'int'); ``` | Old | New | Difference | |-|-|-| | 108 | 2227 | +1962% | | 62 | 90 | +45% | | 2332685 | 3636611 | +56% | | 1625586 | 2639407 | +62% | | 111 | 4559 | +4007% | | 766 | 3675103 | +479678% | | 2347434 | 3669210 | +56% | | 2291298 | 3654664 | +60% | | 3862 | 8003 | +107% | | 2776782 | 3623213 | +30% | | 3171980 | 3870687 | +22% | The new algorithm seems to be faster in all cases. Even in the worst case (compared to the old implementation) it is faster, likely because there is no allocation happening. The larger the haystack the more dramatic the difference. There is *some* variance when executing the benchmark but the difference in the benchmark is way above the margin of error.

« previous php.bugs (#238685) next »