简体   繁体   English

将数组转换为对象或 stdClass(),哪个在 PHP 中更快?

[英]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?鉴于以下函数在 PHP 中更快或更好用,为什么?

Casting an array to object:将数组转换为对象:

<?php

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

    return $obj;
}

Instantiating an stdClass() :实例化一个stdClass()

<?php

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

    return $obj;
}

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不同版本 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奇怪的是 PHP7.1.0 似乎报告了一个比 PHP7.0.13 慢得多的new stdClass() method

Conclusion: Using the $obj = (object) ['prop1' => 1, 'prop2' => 2];结论:使用$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.但是,在使用 PHP7 出现可记录的差异之前,我必须循环 10,000,所以我很确定还有更重要的事情需要担心。

UPDATE The previous results were generated with XDEBUG turned on. UPDATE前面的结果是在打开 XDEBUG 的情况下生成的。 Without XDEBUG it turns out the differences are even less significant and the run times are factor of 10 quicker!如果没有 XDEBUG,结果差异就更小了,运行时间快了 10 倍!

**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

Benchmark基准

Using the two methods:使用两种方法:

<?php

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();

for ($i = 0; $i < 1000000; $i++) {
    // method
}

$time_end = microtime_float();

$time = $time_end - $time_start;

var_dump($time);

with this as the array method:以此作为array方法:

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

and this as the new method:这作为new方法:

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

Results结果

I have:我有:

float 0.085909843444824 (for the `array` method)
float 0.16712999343872 (for the `new` method)

That is, the array method is much faster!也就是说, array方法要快得多!

Repetition重复

Running this code again and again don't change much results.一次又一次地运行这段代码不会改变太多结果。

using this test使用这个测试

<?php
$a = microtime();
$obj = (object) ['prop1' => 1, 'prop2' => 2]; 
$b = microtime();
print_r($b-$a);


$a = microtime();
$obj = new stdClass(); 
$obj->prop1 = 1; 
$obj->prop2 = 2;
$b = microtime();
print_r($b-$a);
?>

i get for first 2.4E-5 and for second 1.0E-5 second faster also is more readable and understandable.我得到第一个2.4E-5和第二个1.0E-5秒更快也更具可读性和可理解性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM