Jay Blanchard wrote:
[snip]
strpos() : 0.18918436765671
preg_match() : 0.26665662288666
[/snip]

1/3rd of a second slower....interesting. You know what though, my
original assertion is correct....almost any of the regex functions will
work.

Tristan, why not convert the string to an array and then use in_array()?
John, can you add the array speed test to your script? I'd like to know
how that stacks up too.


Okay, below is the new script...test will take a few to run. I'm doing the explode of the haystack outside of the test, so it's only going to test the speed of in_array(), and not take into account how long it would take to create the array from a string. I'll post the result in a few.


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/";
$array = explode ( " ", $haystack );
$strpos_temp = 0;
$preg_match_temp = 0;
$in_array_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();
for ( $i = 0; $i < 100000; $i++ ) {
in_array ( $needle, $array );
}
$t4 = getmicrotime();
$strpos_temp += $t2 - $t1;
$preg_match_temp += $t3 - $t2;
$in_array_temp += $t4 - $t3;
}
$strpos = $strpos_temp / $j;
$preg_match = $preg_match_temp / $j;
$in_array = $in_array_temp / $j;
echo ( "strpos() : " . $strpos . "<br />\npreg_match() : " . $preg_match . "<br />\nin_array() : " . $in_array );


--
John C. Nichel
KegWorks.com
716.856.9675
[EMAIL PROTECTED]

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to