📜 ⬆️ ⬇️

Echo or print?

The question: “Which is faster - echo or print?” Does not cease to excite the minds of those who consider themselves to be a professional PHP programmer. The user manual carefully mentions the difference, but refers to a third-party article ; This, in turn, claims that echo is faster, but provides no evidence; it is difficult to find an objective comparison, and all this gives the problem a certain mystical aura, to which many and many peck.

So let's not stand aside and try once and for all to solve this riddle!


')
The behavioral difference between echo and print is that the latter can behave like a function (always returning one); as a result, print can be used in the context of, for example, a ternary operator, which can sometimes be quite attractive. Echo as a function cannot be used, but it can take several arguments separated by commas, and they cannot be bracketed; whereas print has exactly one argument, and it can be either in brackets or not.

However, information about these differences is available in the same manual, we have as our aim to compare performance and, I am not afraid of this word, the speed of these two language constructs.

For the tests, I made a stand where 1000 lines are displayed 1000 times (32 characters each); one of the five methods is used:

  1. print "$value<br />";
  2. echo "$value<br />";
  3. print $value . '<br />';
  4. echo $value . '<br />';
  5. echo $value, '<br />';


For each of the methods of testing were carried out 20 times, with each time recorded time. The distribution of results for all methods turned out to be normal, which allows us to take the average value as an indicator of speed.

The following values ​​were obtained:

  1. 1,727 seconds
  2. 1,727 seconds
  3. 1,462 s
  4. 1,428 seconds
  5. 1,321 s


Indeed, we see that print may turn out to be a little slower than echo (or it may not be that the results of the first two methods demonstrate). At the same time, using echo with several parameters instead of concatenation (and single quotes instead of double quotes) gives a very noticeable performance gain.

But wait, is it really so tangible? Let's count. The difference between the minimum and maximum time is 406 ms per million operations. That is, as it is now fashionable to say, 406 nanoseconds per operation .

What does this give us on a scale - let's say, “VKontakte”? According to LiveInternet, 500 million pages are viewed daily.

How many times is echo or print called for each page? Modern successful sites necessarily resort to any high level of logic abstraction from presentation, using templates, MVC and other scary words; as a result, the output operation is called once per page or, at most, once for each sense block of the page. Suppose the worst option and estimate the number of such blocks in 10 pcs. one page.

It is easy to calculate that saving server time will be at best up to 200 seconds per day , or 0.23%. Is it a lot or a little? Decide for yourself. But first, think about whether optimizing a single SQL query or caching one file is several times greater performance gains.

And I will tell you how long it took me to resolve this issue:

  1. View information in the manual - 2 minutes.
  2. Drawing up a benchmark - 31 minutes.
    A series of trial experiments - 5 minutes.
    Correction of techniques and methods of testing - 20 minutes.
    Testing - 9 minutes.
    Processing results - 5 minutes.

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


All Articles