From: Operating system: OS X 10.6.8 PHP version: 5.3.6 Package: Performance problem Bug Type: Bug Bug description:strpos() performs poorly when given start index
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 bug report at https://bugs.php.net/bug.php?id=55254&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=55254&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=55254&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=55254&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=55254&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=55254&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=55254&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=55254&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=55254&r=needscript Try newer version: https://bugs.php.net/fix.php?id=55254&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=55254&r=support Expected behavior: https://bugs.php.net/fix.php?id=55254&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=55254&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=55254&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=55254&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=55254&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=55254&r=dst IIS Stability: https://bugs.php.net/fix.php?id=55254&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=55254&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=55254&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=55254&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=55254&r=mysqlcfg