Re: IF statement question...

From: Date: Wed, 19 May 2004 14:40:32 +0000
Subject: Re: IF statement question...
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-186534@lists.php.net to get a copy of this message
* Thus wrote Oliver Hankeln (oha@walter-interactive.de): > 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() > ... > > $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(); Make sure your benchmarks aren't bias: - assignment takes time - concating string takes time strpos($haystack,$needle); v.s. preg_match($regex,$haystack); Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#186534) next »