note 27674 deleted from function.str-replace by alindeman
| From: | alindeman@php.net | Date: | Sat, 17 May 2003 22:07:43 +0000 |
| Subject: | note 27674 deleted from function.str-replace by alindeman | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-48291@lists.php.net to get a copy of this message | ||
Note Submitter: eloff at deletethisword helpmygame.com
----
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);
}
?>