* Thus wrote Oliver Hankeln ([EMAIL PROTECTED]): > 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." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php