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

Asked

Viewed 188 times

12

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

I know that to use the 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 am 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 once and I’m losing performance.

  • 2

    related: https://answall.com/questions/138003/qual-%C3%A9-a-finality-do-m%C3%A9todo-m%C3%A1gico-clone

  • 2

    The important thing is the purpose of each, not speed. You have to use clone when you want to clone, and new when you want a new instance. These are not substitute functions. The clone will have to copy the vtable including all values, the new will call the constructor. As already very well answered by @Niero, no matter which is less or more performative, because this is irrelevant in context. Until pq if you are using PHP and want performance, using OOP is already the first mistake. OOP does not bring benefits in PHP and generates an absurd cost, even more that it is a scripting language, which by nature does not maintain state.

2 answers

12


It doesn’t matter. For two reasons.

PHP is a language of script

So if the performance is important, the language is wrong. You have an absurdly greater gain than worrying 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 ills.

This is a classic case that the rest of the people around you are consuming a lot more time and it won’t make a significant difference. Especially in a Singleton that will not run several times (and I didn’t get into the merit that Singleton in PHP is a cannon to kill bird, ie, creating a simpler architecture will give a much better performance than worrying about it). PHP is extremely inefficient.

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

They ran a test, but I reluctant a post because I know how the human brain works and will consider it as something useful, when in fact it is not. Note that uses an old version, we do not know the exact conditions and was very poorly measured since the cost of starting and stopping the clock so many times should be interfering much more in the result. Almost all the tests of benchmark that I’ve seen being made 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 that is not always obvious. So creating a new object the way you’re seeing it there in the code is usually the solution. The biggest problem with cloning something, and until you know which one is slower, is because maybe it does things that you don’t even expect it to do, and we’d be comparing apples to oranges.

Actually even this can become a problem. Not to mention 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 "redoes" the instance yes. It creates a new object and makes a copy.

Your case of low performance

Very likely the bad performance comes from another point and reason. 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’re doing, and measure it properly. If none of this solves, and should, then change language. But changing the language and not solving these other problems will help very little.

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

1

The question you must ask yourself is whether you need to instantiate so many classes. Modify to clone() will not bring visible advantage as it would be a microtimization and without guarantee that it would be 0.01s faster.

In the tests that are out there is usually done in CLI mode with 100,000 iterations. In practice, PHP will run as fcgi or apache module, etc., where the result will be varied depending on the environment and running 1 iteration most of the time. In short, it is a total waste of time to think about this kind of optimization. Perhaps a redesign of logic can bring something effective. I have done remodeling that improved 550% and another 1500% performance and in none of them I worried about microtimization. It was purely a remodel. 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 prick of galaxies. It could be stolen too. It was a mere example.

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

Want another example? Open a Sony TV. You will find inside it, components of competitors like Sharp, Panasonic, Toshiba, LG. So you ask yourself, "Is that a Sony TV?" Anyway, it doesn’t matter what technology you use. What matters is the end result.

Consider an optimization of architecture and strategic use of various technologies intelligently, thinking also about the business model ($), rather than about point microtimizations or ideological issues and personal tastes. By the way, you can’t even consider microtimizations like the one you asked about New() vs Clone(). Or even worse, insane radicalizations like "sucks, swaps everything mimimi php mimimi", which you’ll probably hear around and solve nothing.

Being quite objective:

  • Plan architecture optimization as a whole by doing renovations.
  • The plan should consider the business model For example, how much the joke will cost your company or your employer/client?
  • Achieve a satisfactory result rationally, economically and quickly.

Browser other questions tagged

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