Is there a performance difference between "new" and "clone" in PHP?

10

What is the advantage of using clone instead of new to create an object in PHP?

I know that to use clone it is necessary to pass an instantiated object. But because it is not necessary to "redo" the instance there is significant performance gain?

I'm thinking of applying this to a method of my Singleton class to return copies of the stored instances. I'm dealing with many objects at the same time and I'm losing performance.

    
asked by anonymous 16.09.2018 / 15:34

2 answers

11

It does not matter. For two reasons.

PHP is a script language

So if the performance is important, the language is wrong. You'll have an unbelievably greater gain than worry about it. As Donald Knuth says:

  

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil

     

We must forget small inefficiencies in 97% of the time: premature optimization is the root of all evil.

This is a classic case that the rest around it consumes much more time and this will not make significant difference. Especially in a Singleton that will not run multiple times (and I did not even merit that Singleton in PHP is a cannon to kill bird, that is, creating a simpler architecture will give a much better performance than worry about it). PHP is extremely inefficient.

And I've seen PHP have absurd performance differences from one version to another, and without criteria, so what may be valid one day may not be in the other.

They did a test , but I relied on a post because I know how the human brain works and will consider it useful , when in fact it is not. Note that it uses an old version, we do not know the exact conditions and it was very badly measured since the cost of starting and stopping the clock so many times must be interfering much more in the result. Almost all of the benchmark tests I've seen being done in PHP are wrong.

Different semantics

The second reason is that they do very different things. Almost always cloning something is a mistake and has implications that few people dominate. There is a specific semantics of how the object will be copied which is not always obvious. So creating a new object the way you're looking at it in code is often the solution. The biggest problem with cloning something, and even knowing which is the slowest, is because maybe it does things you do not even expect it to do, and we would be comparing apples to oranges.

In fact, even this can become a problem. Not to mention that having these objects can be an unnecessary complication, but this is another matter.

It's even complicated to measure accurately in PHP, and the results are often inconsistent and measure things other than this particular point.

And there is a wrong premise in the question. The clone "redo" instance yes. It creates a new object and makes a copy.

Your Low-Performance Case

Most likely bad performance comes from another point and motive. Just start thinking about improving something's performance when you can prove it's what's causing the loss.

Again, simplify your code and architecture, understand what you are doing, and measure properly. If none of these solve, and should, change language there. But changing the language and not solving these other problems will help very little.

Test PHP with faster language. If the test is done properly in the right things you will see an absurd difference. Although PHP addresses certain issues, when performance is the most important, it is inadequate, this is measurable, it is not a matter of opinion.

    
16.09.2018 / 16:07
1

The question you have to ask yourself is if you need to instantiate so many classes. Modifying to clone() will not bring visible advantage as it would be a micro-optimization and with no guarantee that it would be 0.01s faster.

In tests that are out there is usually done in CLI mode with 100 thousand iterations. In practice, PHP will run as fcgi or apache module, etc., where the result will vary according to the environment and running 1 iteration most of the time. In short it is a total waste of time to think about this type of optimization. Maybe a remodeling of logic can bring something effective. I've done remodeling that improved 550% and other 1500% performance and in none of them I worried about micro-optimizations. It was purely remodeling. Often the environment change already brings a significant advantage, for example, if it is in apache module, switch to fcgi in nginx. Anyway, there is no magic and I'm not saying that fcgi in nginx is the pica of the galaxies. It can be stolen too. It was a simple example.

I program in C # also and sometimes use PHP to solve certain things where C # is "worse" and vice versa. There is no magic where one solves everything. I use too much JAVA also to solve certain things that in PHP would be slower or inconvenient. So I just invoke a JAVA executable or another language within PHP.

Want another example? Opens a Sony TV. You will find within it competing components like sharp, panasonic, Toshiba, LG. Then you ask yourself "is this a Sony TV?" Anyway, it does not matter what technology you use. What matters is the end result.

Consider optimizing architecture and strategic use of various technologies smartly, also thinking about the business model ($$), rather than on point micro-optimizations or ideological issues and personal tastes. In fact, you can not even consider micro-optimizations like the one that asked about New () vs. Clone (). Or even worse, insane radicalizations like "no good, change everything mimimi php mimimi", which will probably hear around and solve nothing.

Being very objective:

  • Plan optimization of the architecture as a whole by remodeling.
  • The plan should consider the business model For example, how much fun will your company or your boss / customer cost?
  • Achieve a satisfactory result in a rational, economical and fast way.
16.09.2018 / 19:51