note 77610 added to function.implode
| From: | kepex at keflog dot com | Date: | Wed, 05 Sep 2007 18:05:24 +0000 |
| Subject: | note 77610 added to function.implode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130222@lists.php.net to get a copy of this message | ||
Hello, someone sometime says me that the use of implode to concatenate strings with variables of
this way:
<?php
$var1 = "http://keflog.com";
$var2 = "?item=onevalue";
implode('',array($var1,$var2,'&option2=othervalue'));
?>
is more efficient that the operator point (.), but isn't.
I made many test with different methods of concatenation and the result is that when using
"correctly" operator point . always the speed of it is more efficient than implode.
When i write "correctly", i mean that we can concatenate this way:
<?php
$var = $var.'another value';
?>
but is less efficient than:
<?php
$var .= 'another value';
?>
then, correctly i mean, the use of the
.= operator when posible.
Here some results of the tests.
<?php //### Adding $var to $x 5000 times. ### ?>
Method 1: $x .= $var;
Total of seconds: 0.0331
Method 2: $x = implode('',array($x,$var));
Total of seconds: 0.6072
Method 3: $x = $x.$var;
Total of seconds: 0.5558
<?php //### Adding two $var to $x 5000 times. ###?>
Method 1: $x .= $var.$var;
Total of seconds: 0.0143
Method 2: $x = implode('',array($x,$var,$var));
Total of seconds: 1.5992
Method 3: $x = $x.$var.$var;
Total of seconds: 3.2310
Method 4: $x .= implode('',array($var,$var));
Total of seconds: 0.0241
Method 5: $x .= $var; $x .= $var;
Total of seconds: 0.0143
<?php //### Storing six $var to $x 5000 times. ###?>
Method 1: $x = $var.$var.$var.$var.$var.$var;
Total of seconds: 0.0140
Method 2: $x = implode('', array($var,$var,$var,$var,$var,$var));
Total of seconds: 0.0295
<?php //### Storing six diferents strings to $x 5000 times. ###?>
Method 1: $x =
'one'.'two'.'tree'.'four'.'five'.'six';
Total of seconds: 0.0106
Method 2: $x =
implode('',array('one','two','tree','four','five','six'));
Total of seconds: 0.0262
<?php //### Storing variables and strings to $x 5000 times. ###?>
Method 1: $x = 'one'.$var.'tree'.$vartwo.'five';
Total of seconds: 0.0305
Method 2: $x =
implode('',array('one',$var,'tree',$vartwo,'five'));
Total of seconds: 0.0196
----
Server IP: 69.147.83.197
Probable Submitter: 189.157.191.208
----
Manual Page -- http://www.php.net/manual/en/function.implode.php
Edit -- https://master.php.net/note/edit/77610
Del: integrated -- https://master.php.net/note/delete/77610/integrated
Del: useless -- https://master.php.net/note/delete/77610/useless
Del: bad code -- https://master.php.net/note/delete/77610/bad+code
Del: spam -- https://master.php.net/note/delete/77610/spam
Del: non-english -- https://master.php.net/note/delete/77610/non-english
Del: in docs -- https://master.php.net/note/delete/77610/in+docs
Del: other reasons-- https://master.php.net/note/delete/77610
Reject -- https://master.php.net/note/reject/77610
Search -- https://master.php.net/manage/user-notes.php