Home > Backend Development > PHP Tutorial > Performance comparison of clone and new in PHP (code example)

Performance comparison of clone and new in PHP (code example)

藏色散人
Release: 2023-04-06 13:36:02
forward
3985 people have browsed it

Clone and new should not be compared together. Their functions are different. But there may be some scenarios where clone or new can be used, so which one should we choose at this time?

I wrote two tests, the first is to declare an empty class, and the second is a class with constructors and attributes. In addition, I also added PHP serialization testing.

International practice, directly enter the code, it is clear at a glance.

Code

<?php
define(&#39;TEST_COUNT&#39;, 10000);
  
function test($name, $callable)
{
    $time = microtime(true);
    $callable();
    echo $name, &#39; time: &#39;, microtime(true) - $time, &#39;s&#39;, PHP_EOL;
}
// 空的类 
class A
{
     
} 
function test1()
{
    echo &#39;空的类:&#39;, PHP_EOL;
    $a = new A;
    test(&#39;A clone&#39;, function() use($a){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = clone $a;
        }
    });
    test(&#39;A new&#39;, function(){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = new A;
        }
    });
    $serialize = serialize($a);
    test(&#39;A unserialize&#39;, function() use($serialize){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = unserialize($serialize);
        }
    });
} 
test1(); 
// 带构造方法、属性的类 
class B
{
    public $data;
 
    public function __construct($data)
    {
        $this->data = $data;
    }
} 
function test2()
{
    echo &#39;带构造方法、属性的类:&#39;, PHP_EOL;
    $constructData = [
        &#39;id&#39;            =>  1,
        &#39;name&#39;          =>  &#39;imi 框架牛逼&#39;,
        &#39;description&#39;   =>  &#39;IMI 是一款基于 Swoole 开发的协程 PHP 开发框架,拥有常驻内存、协程异步非阻塞IO等优点。&#39;,
        &#39;url&#39;           =>  &#39;https://www.imiphp.com&#39;,
    ];
    $a = new B($constructData);
    test(&#39;B clone&#39;, function() use($a){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = clone $a;
        }
    });
    test(&#39;B new&#39;, function() use($a){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = new B($a->data);
        }
    });
    $serialize = serialize($a);
    test(&#39;B unserialize&#39;, function() use($serialize){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = unserialize($serialize);
        }
    });
} 
test2();
Copy after login

Run result

Empty class:

A clone time: 0.0015249252319336s
A new time: 0.00090503692626953s
A unserialize time: 0.005108118057251s
Copy after login

Class with constructor and attributes:

B clone time: 0.00072503089904785s
B new time: 0.0015559196472168s
B unserialize time: 0.0084571838378906s
Copy after login

Conclusion

Judging from the test results of the empty class, new has higher performance.

Judging from the test results of classes with construction methods and attributes, the performance of clone is much higher than that of new with construction parameters.

Serialization performance is as worrisome as ever, so don’t use it if you can’t use it.

So, we should use clone where we should, and the performance is not bad.

The above is the detailed content of Performance comparison of clone and new in PHP (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yurunsoft.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template