Performance comparison between clone and new in PHP

Keywords: Programming PHP Attribute

Clone and new should not be put together for comparison. Their functions are different. But there may be some scenarios where you can use clone or new. Which one should we choose at this time?

I have written two tests, the first is to declare an empty class, and the second is a class with construction methods and properties. In addition, I added PHP serialization test.

International practice, directly on the code, at a glance.

Code

<?php
define('TEST_COUNT', 10000);
 
function test($name, $callable)
{
    $time = microtime(true);
    $callable();
    echo $name, ' time: ', microtime(true) - $time, 's', PHP_EOL;
}

// Empty class

class A
{
    
}

function test1()
{
    echo 'Empty class:', PHP_EOL;
    $a = new A;
    test('A clone', function() use($a){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = clone $a;
        }
    });
    test('A new', function(){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = new A;
        }
    });
    $serialize = serialize($a);
    test('A unserialize', function() use($serialize){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = unserialize($serialize);
        }
    });
}

test1();

// Class with construction method and attribute

class B
{
    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }
}

function test2()
{
    echo 'Class with construction method and attribute:', PHP_EOL;
    $constructData = [
        'id'            =>  1,
        'name'          =>  'imi Frame bull drive',
        'description'   =>  'IMI It's based on Swoole Development process PHP Development framework, with resident memory, asynchronous and non blocking process IO And so on.',
        'url'           =>  'https://www.imiphp.com',
    ];
    $a = new B($constructData);
    test('B clone', function() use($a){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = clone $a;
        }
    });
    test('B new', function() use($a){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = new B($a->data);
        }
    });
    $serialize = serialize($a);
    test('B unserialize', function() use($serialize){
        for($i = 0; $i < TEST_COUNT; ++$i)
        {
            $obj = unserialize($serialize);
        }
    });
}

test2();

Operation result

Empty class:
A clone time: 0.0015249252319336s
A new time: 0.00090503692626953s
A unserialize time: 0.005108118057251s
 Class with construction method and attribute:
B clone time: 0.00072503089904785s
B new time: 0.0015559196472168s
B unserialize time: 0.0084571838378906s

conclusion

From the test results of empty classes, new has higher performance.

According to the class test results with construction methods and properties, the performance of clone is much higher than that of the construction parameters on the new belt.

Serialization is as bad as ever, so don't use it if you can.

So, we should use clone in a good way. The performance is not bad.

Posted by venradio on Sun, 01 Dec 2019 08:27:46 -0800