Performance in PHP: consider or not?

Asked

Viewed 564 times

33

I see great connoisseurs here of Sopt, saying the phrase: "Want performance, forget PHP"

For example, questions about PHP:

"How can I improve the performance of that?"

Of course we know that "performance" comes a lot from the structure of the code, but this statement "Forget performance in PHP", it would not be because PHP has no performance, but because it is a factor not to be considered when the scenario is PHP compared to another technology, correct!?


So I’d like to know:

  • I believe that when they say that PHP does not focus on its performance, it would be compared to other languages/technologies?

  • This would be about PHP being an interpreted language?

  • Only PHP or all languages interpreted or correlated?


Understanding better the kind of answer I would like, as in the comment made:

This is said because PHP is not meant to be fast. Simple as that. PHP was created to be easy and fast to deliver performance, which in web applications, is not critical. If you need performance do not use PHP, just as if you need to hammer a nail, no use pliers. - Anderson Carlos Woss


Before commenting or responding, understand:

It is not "comparing the best language" or opinion, but rather about the "difference between technologies and/or their limits".

  • Man, I see a lot of gnt talking bad about PHP, use PHP for years and never had problems with performance, certainly there are technologies that have better performance, after all that is the purpose of technological evolution, evolve, at least in my view PHP has life ahead of him yet.

  • @Weessmith It’s not bad talking, it’s about php’s performance being irrelevant about other technologies (I believe, the compiled ones). But of course you’ll always have the "your need" factor, so what’s best for you, regardless of technology.

  • 1

    Who signaled as "mainly based on opinions", the idea is not opinion. It’s about knowing if there really is a difference in performance over PHP technology over another. Mainly by the same speech it would be: "The best technology is the one that serves you". I just wanted to know why they say that, and not opinions. Or please tell me why you consider it an opinion ! :)

  • 1

    Set performance: Run faster even on older machines or is it easier to evolve and maintain or easier to produce? How the quality of the workflow and code produced impact on this perhaps more than the technology used

  • 3

    Your question is too broad to get a satisfactory and objective answer. Performance is the magic word that people use as the final determinant for any discussion about technology, but there are other aspects to consider and each situation is different. Ask a question that focuses on something specific and can be compared without putting language as a whole on trial.

  • @Jean I gave a better example in the question.

  • @Pagotti the question would be yes very broad, but I focused more on "Why do you say that", "Why not focus on performance in PHP", and from what I understand, is that because it is an interpreted language not as many alternatives as another compiled, but not that the language is bad, for no language is.

  • 1

    Just to throw a little more wood on the fire, most of the questions of "How can I improve the performance of that?" focus on micro optimizations almost always irrelevant to the scenario in question. Things like "use an extra variable or not in this case or not ?"

  • @Isac So Isac, but these statements of "Disregard focus on PHP performance" are only in PHP or in all interpreted languages ? It’s because it’s interpreted that it doesn’t have as much emphasis on focusing on performance ?

  • 1

    I suggest you change this title, ta bem clickbait.

  • 2

    This is said because PHP is not meant to be fast. Simple as that. PHP was created to be easy and fast to deliver, giving up performance, which in web applications is not critical. If you need performance don’t use PHP, just like if you need to hammer a nail, don’t use pliers.

  • @Articuno I didn’t even know what "clickbait" was until now ! rs... I liked the title of Woss, but I think it ran away. See

  • @Andersoncarloswoss is exactly what I wanted to know!! But I just had to say, if this is more with PHP or all interpreted languages, or web in general ?

  • 1

    @RBZ is hard to use the word all, but, in general, yes, interpreted languages have lower performance. The same happens with Python, for example.

  • 1

    @Simple andersoncarloswoss. It was that guy. If you can answer, for me it is answered. It is very difficult to ask such a question, it is difficult to specify the purpose of the question, leaving it easy to interpret. I expected downvote, too wide, duplicated. Thank you very much.

  • 1

    Particularly, I liked the question. I’ve had this question before, but I didn’t ask for lack of courage. kkk Thanks RBZ!

  • 1

    @Andreicoelho I’m here sweating hoping not to close ! rs

  • 3

    If you close I will vote to reopen, your question is technical and not opinionated, the question is not how to make any code fast written in PHP (what this would be opinionated), but rather understand why PHP is not considered fast. (ps: I intend to give a slightly different answer from the others, as soon as I have some time to create an explanation as technical as possible, it will probably be more complementary).

  • 1

    @Guilhermenascimento Exactly ! It’s practically: "Why is the performance in PHP not as considered as in other ? Because it is interpreted ?". And the "comparison" with other languages, would be in this focus ! I will definitely be waiting for your answer !

  • Anxiously awaiting the reply of @Guilhermenascimento... =) Usually very good!

Show 15 more comments

2 answers

22


I understood that I was talking about running speed. So the answer is that should not consider performance for micro-optimizations.

Otimização prematura é a raiz de todo mal

Understand that languages do not have speed. At most they have a philosophy that makes it easy or not to have speed. Implementations already have a little more. But not so much. Often the performance is in the library used.

It is possible for something punctual in PHP to be faster than Java. Why? Java has few, but some costs that a lower level language, like C, has. If a PHP function can be written in C, and many of the standard library are, and you don’t have to access much of the PHP virtual execution machine, it’s possible that this function can be faster, as long as the function is very well written. I’m talking about thesis, in practice I don’t see it happening much.

PHP can run faster and with HHVM can improve. Today less, because the PHP implementation has improved a lot. It has several tools that help. But it has other issues that cannot be solved without the language turning into something else. In PHP 8 it will improve more.

Here’s a cruel truth that many PHP amateurs (yes, they love the language) hate to hear: people involved with language development are not the best engineers in the world, do not have a very adequate theoretical basis of languages, and took a complicated legacy, aside from not having the resources necessary to create an extremely fast language.

Note that the benchmarks that show improvements in PHP disregards the load that has worsened a lot by doing a pre-compilation and optimization. When comparing the whole, where PHP is used the whole is much more important than in other types of application, the gain is not so great. Never forget that people who like or produce a technology will use measurements that best meet your needs for marketing, which is legitimate, just do not lose sight of that.

I believe that when they say that PHP does not focus on its performance, it would be compared to other languages/technologies?

Yes, it always has to be compared to other languages. But nobody said that PHP is the slowest language there is. And it improved. PHP is faster than Ruby (standard and current implementation), faster at some things than Python, wins at some Moon points (although Luajit no longer occurs), and loses ugly to Javascript (Nodejs to compare with backend). It doesn’t come close at all when the comparison is with C/C++, C#, Java, D, Rust, Delphi, Go, VB.NET, F#, etc.

This would be about PHP being an interpreted language?

One of the main reasons. But also for having dynamic typing (probably the biggest reason in longer executions), so everything has to be checked internally before executing. Some optimizations are not possible because of this, and for being interpreted as well.

Also by having mechanisms fit all. Essentially it has no diverse data structures, it has the array assoaciative and serves for everything. It even has different structures, but are terribly implemented.

It has a number of misguided decisions that make language not perform that could.

Think that you have a load, a preparation of the environment and the compilation of the text to then execute.

Only PHP or all languages interpreted or correlated?

All following the same philosophy. It is not possible to beat the performance of a code generated by a good compiler and that "everything" is solved at compile time.

It is possible to make interpreted languages have a very interesting final execution and in some cases even beat compiled languages. But it is extremely difficult, costly and the better the execution result the worse the load time will be. Running script this tends not to compensate much.

Hack is PHP compiled and static typing, is much faster. But could more if you didn’t want to maintain good compatibility with PHP.

This is said because PHP is not meant to be fast. Simple as that. PHP was created to be easy and fast to deliver, giving up performance, which in web applications is not critical. If you need performance don’t use PHP, just like if you need to hammer a nail, don’t use pliers.

I agree with the general idea, but performance for web is just not critical for very simple things. What makes sense to use PHP. But it makes no sense to OOP with PHP.

When it’s something with a lot of access, it has a lot to do, performance is critical because it costs more to maintain infrastructure that can handle something slower. Worse still, it may need extremely more complex solutions to distribute the work, and this is very expensive. People don’t even realize they’re paying more than they should. Or that they don’t need the technology they’re using.

I’m not even talking about the opportunity cost of losing a user because it took a long time to respond.

Forget performance in PHP

Obviously this goes for micro-optimization. PHP benefits from macro gains. Choosing how to do can give interesting gains.

Why not focus on PHP performance

Because the gain will be small and the cost will be enormous. Language does not help to gain performance and has cost that has nothing to do that will still be the bulk of the running time. In some languages it is possible to win in 80, 90% of the code path. In PHP it usually gives a maximum of 50, 60%, often less.

Also the fact that most websites have little access and do not need this gain. Just for having little access does not pay to spend a lot on it. Giving performance costs more expensive.

a project that makes access to a database will have the access time to the database much longer than the execution time of the PHP code

This is not always true, especially in PHP codes because it is common for the programmer of this language not understand all the costs. If he does something complex in PHP that could be done in the database, and I see this happening too much, in PHP it will cost more than in C# or Java.

Now, an important issue is that PHP almost invariably accesses database. There are other languages that do not. When there is no IO the performance gain is more important. In fact IO generates a cost that makes performance gains less relevant.

For the answer to be complete I need to quote the Isac comment:

Most of the questions of "How can I improve the performance of this?" focus on micro optimizations almost always irrelevant to the scenario in question. Things like "use an extra variable or not in this case or not?

  • That question could not get his answer. That’s all! rs

  • 1

    So, if by chance I need to create a performance-intensive web application, Node.js, for example, would be a more viable option than php. That’s it Maniero?

  • The comparison I made with database access is not about comparing any code with another more optimized one (in PHP or in the database), it is about concern with the performance of language A or B with respect to the processing time of the database. It’s more in the sense that you put in the I/O.

  • 4

    @Andreicoelho roughly yes, but not always a performance gain results in a direct decrease in costs, and the adoption of a technology you are not familiar with, can introduce new costs. Take a look at this benchmark: Reactphp vs Node.js, leaving a set of 800 req/s to 3000 req/s will really bring a gain to your application?

  • interesting @Tobiasmesquita. I will search more about this. Thank you.

  • 2

    @Andreicoelho has to measure, but can be faster yes. But read the answer carefully, it is not so simple

  • @Andreicoelho. Great benchmark, thanks for sharing. But in the comments there are more benchmarks, in the case using PHP 7. And there is a jump of 4x more req/s

  • @Rafaelsimionatohayashi actually wasn’t me who shared it. But I saw the one you’re talking about. Really has a php library that is very good.

  • 2

    @Rafaelsimionatohayashi the only problem is that it is comparing apples with oranges. Most benchmarks are made to show one side of the thing, one implementation uses a feature that gives one advantage and the other does not use. I’m not advocating anything here unless those comparisons are usually just marketing tools. There are serious tests that show reality, and they have explanations of why that result.

Show 4 more comments

13

Do Not Consider Performance

I decided to put an answer, but it is necessary to anticipate that answers to this type of question will end up being based on the experience of each one with language. For example, if you only work with simple PHP projects or if you worked with older versions, or if you didn’t have the opportunity to migrate from a PHP system to another language or vice versa.

Any attempt to make a performance comparison will need parameters that may be questionable, which may or may not benefit a feature of the language. In the end, there are tools that can pre-compile the code, can optimize the code, can use one command instead of another, etc. Thus, the scope of the question should give this focus, but this is not specified.

As an example for my argument, I can cite a site called Codefights where the goal is to solve problems where people use several different languages and the result is that you learn several different techniques to do the same thing. As the score of the challenges is based on the size of the source code, you realize that even I have something quite optimized often gets unreadable or makes understanding difficult and not always the smallest code is the most efficient or has better performance, it’s just the code with fewer lines. Of course there is a minimum runtime parameter, but the ranking is done by the amount of code written.

If instead of using the size, the site used the performance to make the ranking would end up having the same problem of readability or flexibility to make the solution of the problem more generic, etc. What I mean by this is that we cannot fix just one characteristic of a language to make a judgment of it.

Returning to PHP, you need to know that the language was not born to solve performance problems, in my understanding. It was born with the goal of being a language that could be easy to understand and easy to build a dynamic website in order to simplify both code and installation on a server. That’s why she became popular. But when you combine an easy and flexible language with the programmer’s laziness, the short delivery times, with the normal stages of development that fall into the "first work, then work right, then work right and fast", usually the projects stop in the first phase and the blame ends up with the language.

I understand that the strengths of PHP, although it is known that the latest version had many improvements in code execution, has no main focus on performance. The strengths are linked to the broad code base, frameworks, examples, community, ready-made components, database connectivity, development cycle time for both new functions and maintenance among other features as important as performance.

Putting answers to questions of the question:

I believe that when they say that PHP doesn’t focus on its performance, would be compared to other languages/technologies?

Most likely. The language itself does not generate slow code. It only talks about performance when this feature is essential to the project. To justify a little more the answer, just know that a project that makes access to a database will have much longer access time to the database than the execution time of the PHP code, thus an improvement in the code or exchange for a code in another language will not make the access time to the database decrease.

This would be about PHP being an interpreted language?

Whenever comparing something interpreted with something compiled, the chance of the interpreted performing more is very small.

Only PHP or all languages interpreted or correlated?

According to the argument I put forward, if you take the build aspect and compare only interpreted languages, the performance will depend on the Runtime and in that case, each will have its performance determined when you define the code to be executed. In general, the ones that are done is to run several types of tests and to make an average, having as final a score that can give an idea, but should not be only determinant.

There is an interesting site called The Computer Language Benchmarks Game. You can see the comparative of various languages to make a decision based on the code that has been tested. It’s quite interesting.

Considering this argument, the answer to your question would be not to consider the performance of PHP as the only feature when doing some kind of evaluation, either in research form or for the choice of use in a project.

  • 2

    Pagotti, thank you in advance for your reply, I liked the notes on the 3 questions. On some points he put into question: "...will end up basing themselves on the experience of each with language...", "... Thus, the scope of the question should give this focus, but this is not specified..." The purpose of the question was exactly these 3 questions, looking for an explanation of why these citations on performance appear, and never in comparing experience with language, comparison between languages and much less opinion.

  • Interesting. php on average gets to be "faster" than other web languages by what was presented on the website The Computer... that you posted.

  • 1

    The idea is to know technical reasons, which I think were achieved by Woss and his answer on the 3 questions. I appreciate participating ! + 1

  • PHP Rules! XD... Great answer.

  • 1

    @Andreicoelho but the idea is not the competition for performance, but understand why they say it, if it is because language in a certain scenario, reaches that limit of it.

  • Yes... @RBZ I got it. It was just a comment. =)

  • 1

    It is very difficult to come up with a question in this way, about something that needs an enormous precision of this "non-comparative comparative" point. But I was happy with the result so far ! rs

  • 1

    @RBZ you did very well on that question. It’s really hard.

Show 3 more comments

Browser other questions tagged

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