Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting array to object or stdClass(), which is faster in PHP?

Given the following functions which is faster in PHP or better to use and why?

Casting an array to object:

<?php

function() {
    $obj = (object) ['prop1' => 1, 'prop2' => 2]; 

    return $obj;
}

Instantiating an stdClass():

<?php

function() {
    $obj = new stdClass(); 
    $obj->prop1 = 1; 
    $obj->prop2 = 2;

    return $obj;
}
like image 750
aqua Avatar asked Sep 01 '26 07:09

aqua


1 Answers

My Benchmark for what its worth

function one() {
    $obj = (object) ['prop1' => 1, 'prop2' => 2]; 
    return $obj;
}

function two() {
    $obj = new stdClass(); 
    $obj->prop1 = 1; 
    $obj->prop2 = 2;

    return $obj;
}

$looper = 10000000;

$a = microtime(1);
for ( $i=0; $i < $looper; $i++) { $x = one(); }
$b = microtime(1);
$c = $b-$a;
echo "Using (object) [] method     $looper times " . $c . PHP_EOL;

$a = microtime(1);
for ( $i=0; $i < $looper; $i++) { $x = two(); }
$b = microtime(1);
$d = $b-$a;
echo "Using new stdClass() method $looper times " . $d . PHP_EOL;

echo 'Difference (-ve) means (object) [] is quicker ' . ($c - $d) . PHP_EOL;

Results for different versions of PHP

PHP7.1.0
Using (object) [] method     10,000,000 times 22.970033168793
Using new stdClass() method 10,000,000 times 38.114390850067
Difference (-ve) means (object) [] is quicker -15.144357681274

PHP7.0.13
Using (object) [] method     10,000,000 times 22.230031967163
Using new stdClass() method 10,000,000 times 29.300040960312
Difference (-ve) means (object) [] is quicker -7.0700089931488


PHP5.6.25
Using (object) [] method     10,000,000 times 47.920066833496
Using new stdClass() method 10,000,000 times 54.20007610321
Difference (-ve) means (object) [] is quicker -6.2800092697144


PHP5.5.36
Using (object) [] method     10,000,000 times 46.450064897537
Using new stdClass() method 10,000,000 times 53.110074043274
Difference (-ve) means (object) [] is quicker -6.6600091457367

The odd thing is that PHP7.1.0 seems to report an appreciably slower new stdClass() method than PHP7.0.13

Conclusion: Using the $obj = (object) ['prop1' => 1, 'prop2' => 2]; method does seem to be quicker.

However I have to loop 10,000 before there is a recordable difference using PHP7, so I am pretty sure there are more important things to worry about.

UPDATE The previous results were generated with XDEBUG turned on. Without XDEBUG it turns out the differences are even less significant and the run times are factor of 10 quicker!

**Update for newer releases of PHP**
7.4.12 
Using (object) [] method     10,000,000 times 0.72978782653809
Using new stdClass() method 10,000,000 times 1.3195288181305
Difference (-ve) means (object) [] is quicker -0.58974099159241

7.3.2 
Using (object) [] method     10,000,000 times 0.83337903022766
Using new stdClass() method 10,000,000 times 1.3447999954224
Difference (-ve) means (object) [] is quicker -0.5114209651947

7.0.14
Using (object) [] method     10,000,000 times 2.5900039672852
Using new stdClass() method 10000000 times 3.7700049877167
Difference (-ve) means (object) [] is quicker -1.1800010204315

7.1.0
Using (object) [] method     10,000,000 times 1.8601069450378
Using new stdClass() method 10000000 times 3.215184211731
Difference (-ve) means (object) [] is quicker -1.3550772666931

5.6.28
Using (object) [] method     10,000,000 times 6.0900089740753
Using new stdClass() method 10,000,000 times 6.9300100803375
Difference (-ve) means (object) [] is quicker -0.84000110626221
like image 142
RiggsFolly Avatar answered Sep 03 '26 00:09

RiggsFolly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!