📜 ⬆️ ⬇️

Concatenation of strings is two ways.

Concatenation of lines, or rather the addition of one line to the end of another, can be used in two ways.

1. $ a = $ a. $ b;
2. $ a. = $ B;

Experienced programmers are unlikely to use the first option, but beginners may well.
')
I learned the hard way that the first version took place hundreds of times longer.

For this to show itself, the string $ b must be at least 40 characters. The number of iterations is about 10,000.

Moreover, when studying this issue, it turned out that the syntactic complication or increase in the length of the string being joined strongly influences the speed in the first case, and much less in the second.

Comment by gribozavr :

> .

( ). :

1. , , $a ( $a ). : , , .

2. , , , .


Here is a piece of code that everyone can check for himself:

<? php

$a = NULL;
$b = « , 40»;
$start = microtime(1);

for($i=0;$i<10000;$i++) {
$a = $a. $b;
}

$end = microtime(1);
$time = $end — $start;
echo $time.«
»;

$a = NULL;
$b = « , 40»;
$start = microtime(1);

for($i=0;$i<10000;$i++) {
$a .= $b;
}

$end = getmicrotime(1);
$time = $end — $start;
echo $time;

?>


UPD: At the request of workers I transfer here from a personal blog.

UPD: In the comments in another thread, one comrade ( f33l ) wrote that it was a bug and gave a link:
bugs.php.net/bug.php? id = 44069
Fixed a bug just in version 5.2.6. Although I also have 5.2.6 on the hosting, but there is a “problem”.
The bug says about memory leak, but judging by the comment in the discussion on the topic, it also influenced the execution time.

Source: https://habr.com/ru/post/38754/


All Articles