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.
related: https://answall.com/questions/138003/qual-%C3%A9-a-finality-do-m%C3%A9todo-m%C3%A1gico-clone
– novic
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.
– Bacco