note 77610 deleted from function.implode by nlopess

From: Date: Wed, 05 Sep 2007 18:20:57 +0000
Subject: note 77610 deleted from function.implode by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-130224@lists.php.net to get a copy of this message
Note Submitter: kepex at keflog dot com ---- 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

« previous php.notes (#130224) next »