将数组强制转换为object或stdClass(),在PHP中哪个更快?

将数组强制转换为object或stdClass(),在PHP中哪个更快?,php,arrays,object,casting,stdclass,Php,Arrays,Object,Casting,Stdclass,给定以下函数,在PHP中哪个更快或更好使用,为什么 将数组强制转换为对象: <?php function() { $obj = (object) ['prop1' => 1, 'prop2' => 2]; return $obj; } 使用此测试 <?php $a = microtime(); $obj = (object) ['prop1' => 1, 'prop2' => 2]; $b = microtime(); print_r

给定以下函数,在PHP中哪个更快或更好使用,为什么

将数组强制转换为对象:

<?php

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

    return $obj;
}
使用此测试

<?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
第二个更快的测试也更容易阅读和理解。

使用此测试

<?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
第二个更快的版本也更容易阅读和理解。

Benchmark 使用两种方法:

<?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);
这就是
新的
方法:

$obj = (object) ['prop1' => 1, 'prop2' => 2]; 
$obj = new stdClass(); 
$obj->prop1 = 1; 
$obj->prop2 = 2;
后果 我有:

float 0.085909843444824 (for the `array` method)
float 0.16712999343872 (for the `new` method)
也就是说,
array
方法要快得多

重复 反复运行此代码不会改变太多结果。

Benchmark 使用两种方法:

<?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);
这就是
新的
方法:

$obj = (object) ['prop1' => 1, 'prop2' => 2]; 
$obj = new stdClass(); 
$obj->prop1 = 1; 
$obj->prop2 = 2;
后果 我有:

float 0.085909843444824 (for the `array` method)
float 0.16712999343872 (for the `new` method)
也就是说,
array
方法要快得多

重复
反复运行此代码不会改变太多结果。

我的基准测试

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;
奇怪的是,PHP7.1.0报告的新stdClass()方法似乎比PHP7.0.13慢得多

结论:使用
$obj=(object)['prop1'=>1,'prop2'=>2]方法看起来确实更快

然而在使用PHP7产生可记录差异之前,我必须循环10000次,因此我非常确定还有更重要的事情需要担心

更新 先前的结果是在XDEBUG打开的情况下生成的。如果没有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

我的基准的价值

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;
奇怪的是,PHP7.1.0报告的新stdClass()方法似乎比PHP7.0.13慢得多

结论:使用
$obj=(object)['prop1'=>1,'prop2'=>2]方法看起来确实更快

然而在使用PHP7产生可记录差异之前,我必须循环10000次,因此我非常确定还有更重要的事情需要担心

更新 先前的结果是在XDEBUG打开的情况下生成的。如果没有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@jeroen我的断言是,需要几百万次执行才能注意到我假设的差异。我担心更多的事情important@RiggsFolly我还没有测试过速度,但第二个在我看来更具可读性。特别是当你的数组很长的时候。我不确定,但是:第一个数组没有使用第二个数组吗?是否有额外的步骤?转储运行时并检查哪一个具有最佳的benchmark@jeroen我的断言是,需要几百万次执行才能注意到我假设的差异。我担心更多的事情important@RiggsFolly奇怪的是,我在phpy的所有版本中都得到了相反的结论。你是对的,我在命令行中用MBP运行代码,发现
新的
方法比
数组
慢。。。我编辑我的答案;我将在我的工作场所再次测试这段代码,但可能是我错误地切换了我的结果…我关闭了XDEBUG,速度的提高非常惊人。奇怪的是,我在所有版本的phpy中都得到了相反的结论你是对的,我在命令行中的MBP上运行代码,发现
new
方法比
array
慢。。。我编辑我的答案;我将在我的工作场所再次测试这段代码,但可能是因为我错误地切换了我的结果…我关闭了XDEBUG,速度提高非常惊人。std类非常昂贵,但是,可以使用原型方法,但请在之前检查以下结果:))。放大使用的数据集时会发生什么情况?所以我已经测试过了。使用三倍数据集(6个元素)。对于阵列到对象转换,操作时间没有显著增加(~1-2%)。然而,对于对象(新的和原型),这两种方法的执行时间都显著增加。新标的+50%,原型+70%。所以结论是:随着数据集的增长,使用数组应该是使用数组和强制转换到对象的更好理由。然而,现在出现了一个关于memorystd类代价高昂的问题,然而,我们可以使用原型方法,但请在之前检查以下结果:))。放大使用的数据集时会发生什么情况?所以我已经测试过了。使用三倍数据集(6个元素)。对于阵列到对象转换,操作时间没有显著增加(~1-2%)。然而,对于对象(新的和原型),这两种方法的执行时间都显著增加。新标的+50%,原型+70%。所以结论是:随着数据集的增长,使用数组应该是使用数组和强制转换到对象的更好理由。然而,现在出现了关于记忆的问题