迭代时的性能比较

迭代时的性能比较

本文介绍了PHP对象与数组-迭代时的性能比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量用于神经网络的PHP对象,必须对其进行迭代并对其进行一些数学运算.我想知道在类实例上使用关联数组会更好吗?

I have a huge amount of PHP objects for a neural network for which I have to iterate over and perform some maths on. I was wondering if I would be better off using an associative array over instances of classes?

我正在处理大约3640个对象,并在此之上(最多)迭代500次(最多),因此任何微优化都可以提供很大帮助.难道$object['value']$object->value更快?

I am dealing with around 3640 objects and iterating around 500 times (at best) on top of that so any micro-optimization helps a great deal. Would it inevitably be quicker to do $object['value'] than $object->value?

编辑:因此它们都是相同的.但是我想构造函数会有一些开销吗?无论哪种方式,我都不认为我想用我漂亮的类来交换脏数组:P

So they are both the same. But I guess there would be a little overhead for the constructor? Either way I don't think I want to trade in my beautiful classes for dirty arrays :P

推荐答案

基于Quazzle的代码,我运行了下一个代码(5.4.16 Windows 64位):

Based in the code of Quazzle, i ran the next code (5.4.16 windows 64bits):

<?php
class SomeClass {
    public $aaa;
    public $bbb;
    public $ccc;
    }

function p($i) {
  echo '<pre>';
  print_r($i);
  echo '</pre>';
}


$t0 = microtime(true);
$arraysOf=array();
$inicio=memory_get_usage();
for ($i=0; $i<1000; $i++) {
    $z = array();
    for ($j=0; $j<1000; $j++) {
        $z['aaa'] = 'aaa';
        $z['bbb'] = 'bbb';
        $z['ccc'] = $z['aaa'].$z['bbb'];
    }
    $arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
echo '<p>memory: '.($fin-$inicio)."</p>";
p($z);

$t0 = microtime(true);
$arraysOf=array();
$inicio=memory_get_usage();
for ($i=0; $i<1000; $i++) {
    $z = new SomeClass();
    for ($j=0; $j<1000; $j++) {
        $z->aaa = 'aaa';
        $z->bbb = 'bbb';
        $z->ccc = $z->aaa.$z->bbb;
    }
    $arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
echo '<p>memory: '.($fin-$inicio)."</p>";
p($z);

$t0 = microtime(true);
$arraysOf=array();
$inicio=memory_get_usage();
for ($i=0; $i<1000; $i++) {
    $z = new stdClass();
    for ($j=0; $j<1000; $j++) {
        $z->aaa = 'aaa';
        $z->bbb = 'bbb';
        $z->ccc = $z->aaa.$z->bbb;
    }
    $arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
echo '<p>memory: '.($fin-$inicio)."</p>";
p($z);
?>

我得到了下一个结果:

arrays: 1.8451430797577

memory: 460416

Array
(
    [aaa] => aaa
    [bbb] => bbb
    [ccc] => aaabbb
)

arrays: 1.8294548988342

memory: 275696

SomeClass Object
(
    [aaa] => aaa
    [bbb] => bbb
    [ccc] => aaabbb
)

arrays: 2.2577090263367

memory: 483648

stdClass Object
(
    [aaa] => aaa
    [bbb] => bbb
    [ccc] => aaabbb
)

php 5.4的结论

Conclusion for php 5.4

  1. 与Array相比,Class的速度更快(但略微).
  2. stdClass是邪恶的.
  3. 与数组相比,类使用的内存更少. (大约减少30-40%!)

ps:请注意,如果定义了类,但定义了成员,则使用此类的速度较慢.它还使用更多的内存. 显然,秘密在于定义成员

ps: as a note, if the class is defined but the members then, the use of this class is slower. It also uses more memory. Apparently the secret is to define the members

我从php 5.4更新到了php 5.5(5.5.12 x86 Windows).

I updated from php 5.4 to php 5.5 (5.5.12 x86 windows).

arrays: 1.6465699672699

memory: 460400

Array
(
    [aaa] => aaa
    [bbb] => bbb
    [ccc] => aaabbb
)

arrays: 1.8687851428986

memory: 363704

SplFixedArray Object
(
    [0] => aaa
    [1] => bbb
    [2] => aaabbb
)

arrays: 1.8554251194

memory: 275568

SomeClass Object
(
    [aaa] => aaa
    [bbb] => bbb
    [ccc] => aaabbb
)

arrays: 2.0101680755615

memory: 483656

stdClass Object
(
    [aaa] => aaa
    [bbb] => bbb
    [ccc] => aaabbb
)

php 5.5的结论

  1. 对于数组,PHP 5.5比PHP 5.4快,对于对象,它几乎相同
  2. 由于优化了PHP 5.5和数组,因此类比数组慢.
  3. stdClass是邪恶的.
  4. 与数组相比,类仍然使用更少的内存. (大约减少30-40%!).
  5. SplFixedArray与使用Class类似,但它使用更多的内存.

这篇关于PHP对象与数组-迭代时的性能比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:42