note 27674 added to function.str-replace
| From: | eloff at rack1 dot php dot net | Date: | Sun, 15 Dec 2002 08:08:59 +0000 |
| Subject: | note 27674 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40923@lists.php.net to get a copy of this message | ||
I was curious which was faster, str_replace(), or strtr() in temrs of single character replacement.
I suspected strtr() and was proved right. This script is a little amateur but I suspect it is
atleast fair in terms of comparison because I tested the functions exactly the same way, but first
the results.
Time for first process, strtr(), was 3.26289403439
Time for second process, str_replace(), was 3.82604897022
This was over 1 million trials each, using a while loop on my P4 1.8gHz. There is almost a 20% speed
boost using strtr() for those sigle char replacements.
<?
//benchmark script
$time1 = getMicroTime();
$i = 0;
while ($i < 1000000) {
$str = '192.168.0.75';
$str = strtr($str,'.',"");
$i++;
}
echo "Time for first process, strtr(), was ".(getMicroTime()-$time1)."\r\n";
$time2 = getMicroTime();
$i = 0;
while ($i < 1000000) {
$str = '192.168.0.75';
$str = str_replace('.',"",$str);
$i++;
}
echo "Time for second process, str_replace(), was
".(getMicroTime()-$time2)."\r\n";
//returns seconds since epoch + microseconds, as seconds
function getMicroTime(){
list($usec, $sec) = explode(' ',microtime());
return ((float)$usec + (float)$sec);
}
?>
--
http://www.php.net/manual/en/function.str-replace.php
http://master.php.net/manage/user-notes.php?action=edit+27674
http://master.php.net/manage/user-notes.php?action=delete+27674
http://master.php.net/manage/user-notes.php?action=reject+27674