Re: IF statement question...

From: Date: Wed, 19 May 2004 15:35:20 +0000
Subject: Re: IF statement question...
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-186539@lists.php.net to get a copy of this message
Curt Zirzow wrote:
Make sure your benchmarks aren't bias: - assignment takes time - concating string takes time strpos($haystack,$needle); v.s. preg_match($regex,$haystack);
Just for shits and giggles (and because it's a slow work day), the below script output this... strpos() : 0.18918436765671 preg_match() : 0.26665662288666 ini_set ( "max_execution_time", "72000" ); function getmicrotime() { list ( $usec, $sec ) = explode( " ", microtime() ); return ( (float)$usec + (float)$sec ); } $haystack= "Bob is good."; $needle = "is"; $regex = "/is/"; $strpos_temp = 0; $preg_match_temp = 0; for ( $j = 0; $j < 10000; $j++ ) { $t1 = getmicrotime(); for ( $i = 0; $i < 100000; $i++ ) { $pos = strpos ( $haystack, $needle ); } $t2 = getmicrotime(); for ( $i = 0; $i < 100000; $i++ ) { preg_match ( $regex, $haystack ); } $t3 = getmicrotime(); $strpos_temp += $t2 - $t1; $preg_match_temp += $t3 - $t2; } $strpos = $strpos_temp / $j; $preg_match = $preg_match_temp / $j; echo ( "strpos() : " . $strpos . "<br />\npreg_match() : " . $preg_match . "<br />\n" ); -- John C. Nichel KegWorks.com 716.856.9675 john@kegworks.com

« previous php.general (#186539) next »