RE: [PHP] Fastest commands
| From: | John W. Holmes | Date: | Sun, 27 Apr 2003 14:53:25 +0000 |
| Subject: | RE: [PHP] Fastest commands | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-145425@lists.php.net to get a copy of this message | ||
> Is there a list of commands or any guidance on what executes the
fastest?
> There are some notes in some of the ereg_, preg_ and str_ function
docs
> about which ones are faster, but what about others? Also, I want to
do a
> replace where the search is array indices and the replace is the
values of
> the found index. Is the following the fastest???
>
> $content = "one, two, three";
>
> $thearray = array (
> 'one' => '1',
> 'two' => '2',
> 'three' => '3',
> );
>
> foreach($thearray as $search => $replace) {
> $content = str_replace($search, $replace, $content);
> }
Benchmark it yourself. Every situation is going to be unique. What's
faster for the examples you gave may not be once the arrays start
getting larger.
In my benchmarks for the following values:
$var1 = array('one','two','three');
$var2 = array(1,2,3);
$var3 = array('one'=>1, 'two'=>2, 'three'=>3);
$str = 'one, two, three';
I found this to be the fastest:
$str1 = str_replace($var1,$var2,$str);
Followed by this:
$str2 = str_replace(array_keys($var3),$var3,$str);
Followed by this:
foreach($var3 as $key=>$val)
{ $str = str_replace($key,$val,$str); }
But, each server could be different depending on the actual conditions.
---John Holmes...