gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:02:59 26 4
gpt4 key购买 nike

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

将数组转换为对象:

<?php

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

return $obj;
}

实例化stdClass():

<?php

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

return $obj;
}

最佳答案

我的基准其值(value)

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

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.

更新前面的结果是在打开 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

关于php - 将数组转换为对象或 stdClass(),在 PHP 中哪个更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41021195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com