|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-07-20 18:37 UTC] webmaster at thedigitalorchard dot ca
-Summary: strpos() performs poorly when given start index
+Summary: strpos() performs more poorly when offset specified
[2011-07-20 18:37 UTC] webmaster at thedigitalorchard dot ca
[2011-07-20 19:42 UTC] webmaster at thedigitalorchard dot ca
[2011-07-21 19:48 UTC] dsp@php.net
-Status: Open
+Status: Bogus
[2011-07-21 19:48 UTC] dsp@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 05:00:01 2025 UTC |
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.