1

首先我找到了这个对象 vs 数组,然后我在我的代码中添加了一个 ArrayObject 和扩展的 ArrayObject 。结果很奇怪:扩展的 ArrayObject 在计算时间上接近普通的 ArrayObject。

这是我的测试用例,数组 vs 对象 vs 数组对象 vs 扩展数组对象:

<pre><?

set_time_limit(0);
$times = 2000;

function profiling($tester, $desc)
{
    $start = microtime(true);
    $tester();
    echo "$desc: ",(microtime(true) - $start),"\n";
}

profiling(function()
{
    global $times;
    for ($i=0; $i<$times; $i++) {
        $z = array();
        for ($j=0; $j<$times; $j++) {
            $z['aaa'] = 'aaa';
            $z['bbb'] = 'bbb';
            $z['ccc'] = $z['aaa'].$z['bbb'];
        }
    }
}, 'use array');


profiling(function()
{
    global $times;
    for ($i=0; $i<$times; $i++) {
        $z = (object) null;
        for ($j=0; $j<$times; $j++) {
            $z->aaa = 'aaa';
            $z->bbb = 'bbb';
            $z->ccc = $z->aaa.$z->bbb;
        }
    }
}, 'use object');

profiling(function()
{
    global $times;
    for ($i=0; $i<$times; $i++) {
        $z = new ArrayObject();
        for ($j=0; $j<$times; $j++) {
            $z['aaa'] = 'aaa';
            $z['bbb'] = 'bbb';
            $z['ccc'] = $z['aaa'].$z['bbb'];
        }
    }
}, 'use arrayobject');

profiling(function()
{
    global $times;
    for ($i=0; $i<$times; $i++) {
        $z = new MyArray();
        for ($j=0; $j<$times; $j++) {
            $z['bbb'] = 'bbb';
            $z['ccc'] = $z['aaa'].$z['bbb'];
        }
    }
}, 'use extends arrayobject');

class MyArray extends  ArrayObject
{
    function __construct()
    {
        parent::__construct(array('aaa'=>'aaa'));
    }
}

echo 'phpversion '.phpversion();

在我的电脑上,输出是

use array: 4.1052348613739
use object: 5.6103208065033
use arrayobject: 5.4503121376038
use extends arrayobject: 4.5252590179443
phpversion 5.3.25

排名为:数组 > 扩展数组对象 > 数组对象 > 对象。

为什么extends ArrayObject比 ArrayObject 和 Object 快?

4

1 回答 1

2

这是因为您使用扩展数组对象的函数没有设置 $z['aaa'] 2000 次,但您使用 ArrayObject 的函数是。

如果我添加一个扩展数组对象函数的版本,它确实设置了 $z['aaa'] 结果更加一致:

profiling(function()
{
    global $times;
    for ($i=0; $i<$times; $i++) {
        $z = new MyArray();
        for ($j=0; $j<$times; $j++) {
            $z['bbb'] = 'bbb';
            $z['ccc'] = $z['aaa'].$z['bbb'];
        }
    }
}, 'use extends arrayobject (no aaa)');

/* added MyArray function with $z['aaa'] = 'aaa' added to the loop */
profiling(function()
{
    global $times;
    for ($i=0; $i<$times; $i++) {
        $z = new MyArray();
        for ($j=0; $j<$times; $j++) {
            $z['aaa'] = 'aaa';
            $z['bbb'] = 'bbb';
            $z['ccc'] = $z['aaa'].$z['bbb'];
        }
    }
}, 'use extends arrayobject (with aaa)');

输出如下:

use array: 1.3838648796082
use object: 1.9023339748383
use arrayobject: 2.0339980125427
use extends arrayobject (no aaa): 1.6399688720703
use extends arrayobject (with aaa): 2.040415763855
phpversion 5.4.4-14+deb7u7

请注意,在循环中使用 ArrayObject 的函数和使用带有 $z['aaa'] 的扩展 ArrayObject 的函数的时间更接近。

于 2014-01-01T21:48:52.390 回答