Edit report at https://bugs.php.net/bug.php?id=55254&edit=1
ID: 55254
User updated by: webmaster at thedigitalorchard dot ca
Reported by: webmaster at thedigitalorchard dot ca
-Summary: strpos() performs poorly when given start index
+Summary: strpos() performs more poorly when offset specified
Status: Open
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:
Changed summary title to be more clear (replaced "index" with "offset")
Previous Comments:
------------------------------------------------------------------------
[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