Re: IF statement question...
| From: | Oliver Hankeln | Date: | Wed, 19 May 2004 12:43:27 +0000 |
| Subject: | Re: IF statement question... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-186529@lists.php.net to get a copy of this message | ||
Jay Blanchard wrote:
[snip] Tipp: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. [/snip] This brings up a good point. Just exactly how much faster would one be over another in this small example? How big would the string have to be to note any degredation of performance?I wrote a small script to test this: 100000 Searches in a rather small string took 0.38s with strpos() and 0.55s with preg_match() While this is not too much in absolute time preg_match uses 145% of the strpos time. You can play with the script: <?php $haystack="This is a string. It is not really long, but I hope it is long enough for this test ;-)"; $needle="test"; function getmicrotime() {
$time=microtime();
list($usec, $sec) = explode(" ", $time);
return ((float)$usec + (float)$sec);
}
$t1=getmicrotime();
for($i=0;$i<100000;$i++)
$pos=strpos($haystack,$needle);$t2=getmicrotime(); for($i=0;$i<100000;$i++)
preg_match("/".$needle."/",$haystack);
$t3=getmicrotime();
printf("Time for strpos: %1.2f s<br>",$t2-$t1);
printf("Time for preg_match: %1.2f s<br>",$t3-$t2);
?>
Oliver