note 66519 added to function.strstr
| From: | w3steve at berkeley dot edu | Date: | Wed, 24 May 2006 23:38:41 +0000 |
| Subject: | note 66519 added to function.strstr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111973@lists.php.net to get a copy of this message | ||
Some figures on the performance of strstr vs. strpos
(my application was to test $PHP_SELF to see what set of directories an include file was being
called from)
test code:
<?php
// 24/05/2006 ss
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
// stand-in for $PHP_SELF
$myphpself = "/exampledirectory/index.php";
// run the set several times
for ($t=0; $t < 5; $t++) {
// test overhead
$time_start = getmicrotime();
for ($i=0; $i < 1000; $i++){
if ( $myphpself ) {
//do nothing, 1000 times
}
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "$time seconds for overhead<br>\n";
// test strpos
$time_start = getmicrotime();
for ($i=0; $i < 1000; $i++){
if ( strpos($myphpself, 'example')) {
//do nothing, 1000 times
}
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "$time seconds to test strpos 1k times<br>\n";
// test strstr
$time_start = getmicrotime();
for ($i=0; $i < 1000; $i++){
if ( strstr($myphpself, 'example')) {
//do nothing, 1000 times
}
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "$time seconds to test strstr 1k times<br>\n";
// test strpos !== FALSE
$time_start = getmicrotime();
for ($i=0; $i < 1000; $i++){
if ( strpos($myphpself, 'example') !== FALSE) {
//do nothing, 1000 times
}
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "$time seconds to test strpos !== FALSE 1k times<br>\n<br>\n";
}
?>
Results:
0.00107002258301 seconds for overhead
0.00192594528198 seconds to test strpos 1k times
0.00201010704041 seconds to test strstr 1k times
0.00249099731445 seconds to test strpos !== FALSE 1k times
0.00104904174805 seconds for overhead
0.00189995765686 seconds to test strpos 1k times
0.00197291374207 seconds to test strstr 1k times
0.00248503684998 seconds to test strpos !== FALSE 1k times
0.0010449886322 seconds for overhead
0.00186109542847 seconds to test strpos 1k times
0.00201511383057 seconds to test strstr 1k times
0.00245690345764 seconds to test strpos !== FALSE 1k times
0.000998020172119 seconds for overhead
0.00183892250061 seconds to test strpos 1k times
0.00199389457703 seconds to test strstr 1k times
0.00246500968933 seconds to test strpos !== FALSE 1k times
0.0010449886322 seconds for overhead
0.00184917449951 seconds to test strpos 1k times
0.00199913978577 seconds to test strstr 1k times
0.00242614746094 seconds to test strpos !== FALSE 1k times
----
Server IP: 216.194.113.175
Probable Submitter: 66.117.145.217
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.strstr.php
Edit -- http://master.php.net/note/edit/66519
Del: integrated -- http://master.php.net/note/delete/66519/integrated
Del: useless -- http://master.php.net/note/delete/66519/useless
Del: bad code -- http://master.php.net/note/delete/66519/bad+code
Del: spam -- http://master.php.net/note/delete/66519/spam
Del: non-english -- http://master.php.net/note/delete/66519/non-english
Del: in docs -- http://master.php.net/note/delete/66519/in+docs
Del: other reasons-- http://master.php.net/note/delete/66519
Reject -- http://master.php.net/note/reject/66519
Search -- http://master.php.net/manage/user-notes.php