将数组转换为object或stdClass(),这在PHP中更快?

时间:2016-12-07 15:22:20

标签: php arrays object casting stdclass

考虑到以下在PHP中更快或更好使用的功能以及为什么?

将数组转换为object:

<?php

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

    return $obj;
}

实例化stdClass()

<?php

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

    return $obj;
}

3 个答案:

答案 0 :(得分:2)

我的基准的价值

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;

不同版本的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
  

奇怪的是,PHP7.1.0似乎比PHP7.0.13报告明显慢new stdClass() method

     

结论:使用$obj = (object) ['prop1' => 1, 'prop2' => 2];方法似乎更快。

     

然而我必须在使用PHP7之前有可记录的差异之前循环10,000,所以我很确定有更重要的事情需要担心。

<强>更新 之前的结果是在XDEBUG打开时生成的。没有XDEBUG,事实证明差异甚至更小,运行时间更快10倍!

7.0.14
Using (object) [] method     10000000 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     10000000 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     10000000 times 6.0900089740753
Using new stdClass() method 10000000 times 6.9300100803375
Difference (-ve) means (object) [] is quicker -0.84000110626221

答案 1 :(得分:1)

基准

使用这两种方法:

<?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);

将此作为array方法:

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

这是new方法:

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

结果

我有:

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

也就是说,array方法要快得多!

重复

一次又一次地运行此代码不会改变很多结果。

答案 2 :(得分:0)

使用此测试

<?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);
?>

我先获得2.4E-5和第二个1.0E-5 第二快也更易读和易懂。