Concatenation or data sequencing: which performs best?

Asked

Viewed 247 times

6

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

Printing of data via echo PHP is widely used when developing applications. In a situation where you receive millions of requests on the server all the time, then you need to get the maximum server performance, which would be the best way to display different data in the same function?

<?php

  #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 what this practice is really called. Remembering that as the echo is not actually a function, according to the PHP manual, so it would not be considered 2 parameters in fact.

echo is not a function currently (language constructor) so parentheses are not required. 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 need to be in parentheses.

  • Another example was missing: $frase = "Bem vindo, {$nome}"

  • 1

    Generally, it is not relevant. Just testing to know. And it depends on version, of the interpreter used.

  • 2

    Let me tell you something, this is micro-optimization, it doesn’t affect anything sometimes, there are things that we do in our code besides this, like create huge classes or include classes and functions without need that these yes can have performance problems for the final script. Concactenar strings will rarely make any impact, only in a very long "stress" test that will feel some variation and yet it will be minimal what is not worth the trouble. An interesting thing to use in production is Opcache or Xcache: https://answall.com/a/166747/3635

3 answers

8

Whenever speaking in language performance, it makes sense to discuss this when programming in Assembly, C, C++, even C#, Java, who knows 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 the language. She is a language of script. If you’re worried about meeting millions of requests (which don’t usually happen) and you think that language plays a key role in this, more than algorithms that are well done or that you’ve done everything you can in algorithms, then this isn’t going to solve any problems, opt for a more scalable language.

Whenever you want to know what has better performance you have to measure. Measure right. Know that you can change according to language implementation. It’s not something that will always be the same.

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

I think passing multiple parameters should be the fastest because it just needs to read the characters and send 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 copying the two data and then reading all the characters together to send to the output. That is, if you don’t have any optimization, which I don’t think you do.

Interpolate string, if there are no optimizations, it may be faster than concatenation, but it will still depend on a certain interpretation. If the interpreter does not turn the interpolation into a sequence, there will be a certain overhead, not to mention his own overhead generated by the interpretation of what is text and what is code inside.

Note that even the use of single or double quotes will affect the performance of the interpretation.

7


To make it clear: as stated by Maniero and by William, has many other factors that influence performance more than this detail. If you have performance issues in your project, that’s not what will save you.

Out of curiosity, using the site 3v4l.org, ran some tests.

Test 1 - echo with multiple constant values (link)

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

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

inserir a descrição da imagem aqui

Verifying the analysis of OP Codes provided, we have:

Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/e38n0
function name:  (null)
number of ops:  4
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   3     0  E >   ECHO                                                     'Anderson'
         1        ECHO                                                     'Carlos'
         2        ECHO                                                     'Woss'
         3      > RETURN                                                   1

That is, when executed in this way, what actually happens is the call of instruction echo three times, each with a parameter.

Test #2 - echo with constant values concatenation (link)

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

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

inserir a descrição da imagem aqui

Verifying the analysis of OP Codes provided, we have:

Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/hQmo3
function name:  (null)
number of ops:  2
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   3     0  E >   ECHO                                                     'AndersonCarlosWoss'
         1      > RETURN                                                   1

What seems to happen in this case is the PHP interpreter already concatenates the values before the code is executed, so the statement echo is called only once, already with the string end. That’s because past values are constant and the interpreter can do such optimization (if it can be called like this).

Test #3 - echo with multiple variable values (link)

$nome = "Anderson";

echo "Bem vindo, ", $nome;

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

inserir a descrição da imagem aqui

Verifying the analysis of OP Codes provided, we have:

Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/N4IMI
function name:  (null)
number of ops:  4
compiled vars:  !0 = $nome
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   3     0  E >   ASSIGN                                                   !0, 'Anderson'
   5     1        ECHO                                                     'Bem+vindo%2C+'
         2        ECHO                                                     !0
         3      > RETURN                                                   1

Again we see that what actually happens is the duplicate call of echo, one with constant value, another with variable defined.

Test #4 - echo with variable concatenation (link)

$nome = "Anderson";

echo "Bem vindo, ". $nome;

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

inserir a descrição da imagem aqui

Verifying the analysis of OP Codes provided, we have:

Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/FfLnC
function name:  (null)
number of ops:  4
compiled vars:  !0 = $nome
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   3     0  E >   ASSIGN                                                   !0, 'Anderson'
   5     1        CONCAT                                           ~2      'Bem+vindo%2C+', !0
         2        ECHO                                                     ~2
         3      > RETURN                                                   1

Now having a variable involved, we can notice that the process of "optimization" occurred previously is no longer possible, and thus occurring, in fact, the call of the concatenation operator.

I don’t know how conclusive this data can be, but it is possible to verify that the concatenation process has shown to be faster than the multiple calls of echo when several values are passed separated by a comma.

5

This is micro-optimization, does not affect at all times, there are things we do in our codes beyond this, like create huge classes or include classes and functions without need that these yes may have performance problems for the final script.

Concatenating strings will rarely make any impact, only on one "stress test" well over that will feel some variation and yet it will be minimal what is not worth taking the trouble.

Why echo wears comma

The reason for echo use , it is not for performance reasons, either that on a machine or php version in a very heavy stress test echo 'a'.'b'; can eventually be better than echo 'a', 'b'; and the next test on the same machine may not, it will depend on the timing, the same reason is to facilitate the use other things like doing a mathematical operation, see the difference:

<?php
echo '0', 5 + 2, '3', PHP_EOL; //Resulta em 073

echo '0' . 5 + 2 . '3', PHP_EOL; //Resulta em 73

Then note that the zero ended up being "confused" with part of the mathematical operation in the second echo

This is just one example, there are many more situations where using the comma will be much more practical and may make it easier for you.

Concluding

About your question

In a situation where you receive millions of requests on the server all the time, then you need to get the maximum server performance, which would be the best way to display different data in the same function?

Answering the question something like this echo 'foo' . $bar; against echo 'foo', $bar; there will be no difference at all perseptive, what can affect and what usually affects in this issue may be numerous other things, as things that may often perhaps be unnecessary (which should only happen sometimes, but you end up doing perform almost always):

  • Giant frameworks for simple things like using Laravel or cakephp to create a simple 3-page website
  • Executions of "requests", how to access in a database and do nothing, not even a SELECT or UPDATE
  • Start functions without need or on page that are not required, such as starting a session_start on a page that does not require a session

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

How to improve the performance of my scripts

Outside you avoid the situations I commented above, another interesting thing to use on producing (do not use in development environments) is Opcache or Xcache:

PHP has nothing like JIT (Just In Time) natively, however there is the extension Opcache which can be enabled on PHP5.6+ servers (for earlier 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 request 1000 each and then run them, but with Opcache (or other alternatives like Xcache) to interpretation of the code is in a type of "cache", ie is interpreted "once" (approximately) and stays in "cache" for some time, so the next requests will only run.

Opcache (or others) at least in tests that I have done greatly improve, even in a test with Apachebench (tool to simulate multiple simultaneous requests) greatly improved the response time, I executed the following command:

Sem Opcache:

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

With Opcache:

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

It seems the number hasn’t changed, but do the math 2350 - 2176 = 174, the Opcache managed 174 requests in a second more than without Opcache, ie it is as if 174 more people had been able to access that page in just 1 second, now imagine in an hour (of course this is all relative).

The test was done with a very simple script, if it had classes, includes, connection to bank, you can even notice a bigger improvement, follow the test script:

<?php

$nome = 'Stack OVerflow';

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

echo memory_get_peak_usage() / 1024, 'Kb';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.