Concatenation or sequencing of data: Which one performs best?


A few days ago I was writing articles about PHP, in which I was asked about a possible improvement in large scale when developing applications in PHP. The assumption was as follows:

Printing data via echo of PHP is extremely used at the time of application development. In a situation where you get millions of requests on the server at all times, then you need to get the maximum performance from the server, what would be the best way to display distinct data in the same function?


  #método 1 - Exibindo dados em sequência
  $nome = "Daniel";
  echo "Bem vindo, ", $nome; // mostra na tela: "Bem vindo, Daniel"
  #método 2 - Usando concatenação
  $nome = "Daniel";
  $frase = "Bem vindo, ".$nome;
  echo $frase;  // mostra na tela: "Bem vindo, Daniel"


I called the method presented by the data sequencing questioner for not knowing exactly how this practice actually calls itself. Remembering that since echo is not really a function, according to the PHP manual, then 2 parameters would not actually be considered.


echo is not a function currently (language constructor) so it is not mandatory to use parentheses. echo (unlike another language constructor) does not behave like a function, so it is not always used in the context of a function. So, if you want to pass more than one parameter to echo, the parameters do not have to be enclosed in parentheses.

asked by anonymous 23.06.2017 / 17:16

3 answers


To make it clear: as stated by bigown and Guilherme , there are many other factors that influence performance more than this detail. If you have performance issues in your project, that will not save you .

Out of curiosity, using the site , I ran some tests.

Test 1 - echo with multiple constant values ( link )

echo "Anderson", "Carlos", "Woss";

The performance result can be seen in different versions of PHP below:




Test#2-echowithconcatenationofconstantvalues( link )

echo "Anderson"."Carlos"."Woss";

The performance result can be seen in different versions of PHP below:



Test#3-echowithmultiplevalueswithvariable( link )

$nome = "Anderson";

echo "Bem vindo, ", $nome;

The performance result can be seen in different versions of PHP below:




Test#4-echowithconcatenationwithvariable( link )

$nome = "Anderson";

echo "Bem vindo, ". $nome;

The performance result can be seen in different versions of PHP below:



Nowhavingavariableinvolved,wecanseethattheprevious"optimization" process is no longer possible, so the call of the concatenation operator is actually occurring.

I do not know how much of this data can be conclusive, but you can see that the concatenation process was faster than multiple echo calls when multiple comma-separated values were passed.

29.06.2017 / 15:17

Whenever you talk about language performance, it makes sense to discuss this when programming in Assembly, C, C ++, even C #, Java, Rust, D, or Delphi.

In PHP the solution is very simple if you need language performance. Change language. This is not the strong point of language. It is a script language. If you are worried about millions of requests (which usually do not happen) and you think that language plays a fundamental role in this, rather than well-done algorithms or that you have done everything you could in the algorithms, then that will not solve a problem, choose a more scalable language.

Whenever you want to know what performs best you have to measure. Measure right. Knowing that you can change according to language implementation. It is not something that will always be the same.

Without knowing the exact implementation of these forms I will kick based on my knowledge of computing and how languages are often implemented.

I think passing several parameters should be faster because it just needs to read the characters and send them to the output in a simple way. Note that the output should be absurdly slower than reading the data, so it should make little difference.

Concatenate has to create a new string by copying the two data and then reading all the characters together to send to the output. That is, if you do not have any optimizations, which I do not think you have.

Interpolating string , if it does not have optimizations, may be faster than concatenation, but still depend on some interpretation. If the interpreter does not transform the tween into a string, there will be a certain overhead , without speak in the overhead generated by the interpretation of what is text and what is code therein.

Note that even using single or double quotation marks will affect interpretation performance .

23.06.2017 / 18:14

This is micro-optimization , it does not affect anything at times, there are things we do in our code besides this, such as creating huge classes or include classes and functions without necessity that these may have problems performance for the final script.

Matching strings will rarely make any impact, only on a very long stress test that will feel some variation and yet it will be minimal which is not worth the trouble.


Why echo uses comma

The reason for echo using , is not because of performance issues, so much so that in a machine or php version in a very heavy stress test echo 'a'.'b'; might eventually be better which echo 'a', 'b'; and in the next test on the same machine might not, it will depend on the moment , the same reason is to make it easier to use other things like doing a math operation, see the difference: p>

echo '0', 1 + 2, '3', PHP_EOL; //Resulta em 033

echo '0' . 1 + 2 . '3', PHP_EOL; //Resulta em 33

This is just an example, there are a lot more situations where using the comma will be much more practical and can facilitate you.


About your question


In a situation where you get millions of requests on the server at all times, then you need to get the maximum performance from the server, what would be the best way to display distinct data on the same function?

Answering the question something like this echo 'foo' . $bar; versus echo 'foo', $bar; will have no prospective difference, what can affect and what usually affects this issue can be countless other things like things that can often be unnecessary should only happen sometimes, but you end up making it run almost always):

  • Giant frameworks for simple things like using Laravel or cakephp to create a simple 3-page site
  • Execution of "requests", such as accessing a database and doing nothing, not even a SELECT or UPDATE
  • Start unnecessary or pageless functions that are not required, such as starting a session_start on a page that does not need a session

There are more things that can affect, but it is quite relative

How to improve the performance of my scripts

If you avoid the situations I mentioned above, another interesting thing to use in production (not use in development environments) is Opcache or XCache:

PHP does not have anything like Just In Time (natively), however there is the Opcache extension that can be enabled on servers running PHP5.6 + for previous versions you have to install manually or via Pearl and not all servers allow this), PHP is an interpreted language, without 1000 people request a page PHP will reinterpret the scripts for that 1000 request each and then runs them, but with Opcache (or other alternatives such as XCache) the interpretation of the code is in a type of cache , that is interpreted "once "(approximately) and is cached for some time, so the next requests will just execute.

The Opcache (or others) at least in tests that I did greatly improve, even in a test with ApacheBench (tool to simulate multiple simultaneous requests) greatly improved the response time, I ran the following command:

No Opcache:

Requests per second: 2176.80 [#/sec] (mean)

With Opcache:

Requests per second: 2350.93 [#/sec] (mean)

It seems that the number has not changed, but it does the 2350 - 2176 = 174 accounts, Opcache has succeeded 174 requests in a second more than without Opcache, ie it is as if 174 more people could have accessed that page. / p>

The test was done with a very simple script, if it had classes, includes, connection with bank, you would notice a much greater improvement, follow the test script:


$nome = 'Stack OVerflow';

for ($i = 0; $i < 1000; $i++) {
    echo 'Olá ', $nome, '<br>';

echo memory_get_peak_usage() / 1024, 'Kb';
29.06.2017 / 15:42