Edit report at https://bugs.php.net/bug.php?id=55254&edit=1
ID: 55254 Updated by: d...@php.net Reported by: webmaster at thedigitalorchard dot ca Summary: strpos() performs more poorly when offset specified -Status: Open +Status: Bogus Type: Bug Package: Performance problem Operating System: OS X 10.6.8 PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php it's related to parameter passing. Previous Comments: ------------------------------------------------------------------------ [2011-07-20 19:42:49] webmaster at thedigitalorchard dot ca This issue that I've observed may in fact not be related to strpos() at all, but merely the fact that there's one more parameter to parse. Passing in zero "0" as the third parameter gives the same result as any other offset. Removing the parameter restores the performance. In a way, this makes sense, but maybe this highlights an area where PHP/Zend can be improved further -- parameter parsing. Feel free to close this bug report if my assessment of this performance issue is accurate. ------------------------------------------------------------------------ [2011-07-20 18:37:52] webmaster at thedigitalorchard dot ca Changed summary title to be more clear (replaced "index" with "offset") ------------------------------------------------------------------------ [2011-07-20 18:13:46] webmaster at thedigitalorchard dot ca Description: ------------ When strpos() is given an index position to start searching, it actually performs more poorly than when the parameter is left off (thus defaulting to "0"). One would expect that giving it a starting index would improve performance since it could skip checking so many characters. I've been able to reproduce this issue consistently with the attached script. Two loops, each run 1 million times. The first loop specifies a start index of 2, whereas the second one doesn't specify one. You would expect the first loop to run faster. Try reversing this (giving the second loop a start index) and you will see the execution time results flip, as well. My framework makes heavy use of strpos() and I discovered this issue when I attempted to optimize strpos() by skipping the first few characters of the string to be checked. No go. Test script: --------------- <?php $k = 'image:0:{name}.{ext}'; $start = microtime(TRUE); for ($i = 0; $i < 1000000; $i++) { strpos($k, '{', 2); } echo "<p>Seconds: ".(microtime(TRUE) - $start).'</p>'; $start = microtime(TRUE); for ($i = 0; $i < 1000000; $i++) { strpos($k, '{'); } echo "<p>Seconds: ".(microtime(TRUE) - $start).'</p>'; Expected result: ---------------- Specifying a start index would result in better performance since it has less to check. Actual result: -------------- Specifying a start index actually has a negative impact on the performance of the script, possibly due to logic where it must check if the index is positive or negative. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=55254&edit=1