php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55254 strpos() performs more poorly when offset specified
Submitted: 2011-07-20 18:13 UTC Modified: 2011-07-21 19:48 UTC
From: webmaster at thedigitalorchard dot ca Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.3.6 OS: OS X 10.6.8
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: webmaster at thedigitalorchard dot ca
New email:
PHP Version: OS:

 

 [2011-07-20 18:13 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
Changed summary title to be more clear (replaced "index" with "offset")
 [2011-07-20 19:42 UTC] 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-21 19:48 UTC] dsp@php.net
-Status: Open +Status: Bogus
 [2011-07-21 19:48 UTC] dsp@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 30 05:01:31 2024 UTC