PHP对象VS阵列 [英] PHP Objects vs Arrays
问题描述
我为我有遍历并执行一些数学神经网络一个巨大的PHP量的对象。我在想,如果我想使用关联数组在类的实例会更好?
我处理的周围 3640
对象和周围的迭代 500
倍(最好)对,这么顶部任何微型优化帮了不少忙。将它不可避免地会更快做 $对象['值']
比 $对象 - >值
编辑:所以,他们都是一样的。但我想会有的构造一个小的开销?无论哪种方式,我不认为我想在美丽的班脏数组交易:P
根据在Quazzle的code,我跑下一个code(5.4.16窗口64位):
< PHP
类SomeClass的{
公共$ AAA;
公共$ BBB;
公共$ CCC;
}函数p($ⅰ){
呼应'< pre>';
的print_r($ I);
呼应'< / pre>';
}
$ T0 = microtime中(真);
$ arraysOf =阵列();
$ INICIO = memory_get_usage();
为($ I = 0; $ I< 1000; $ I ++){
$ Z =阵列();
为($ J = 0; $ J< 1000; $ J ++){
$ Z [AAA] ='AAA';
$ Z ['BBB'] ='BBB';
$ Z ['CCC'] = $ Z ['AAA'] $ Z ['BBB']。
}
$ arraysOf [] = $ Z者除外;
}
$翅片= memory_get_usage();
呼应'< P>数组:'(microtime中(真) - $ T0)< / P>中;
呼应'< P>内存:'($装订$ INICIO)< / P>中;
P($ Z);$ T0 = microtime中(真);
$ arraysOf =阵列();
$ INICIO = memory_get_usage();
为($ I = 0; $ I< 1000; $ I ++){
$ Z =新SomeClass的();
为($ J = 0; $ J< 1000; $ J ++){
$ Z-GT&; AAA ='AAA';
$ Z-GT&; BBB ='BBB';
$ Z-GT&; CCC = $ Z-GT&; $ z轴&GT AAA,BBB。
}
$ arraysOf [] = $ Z者除外;
}
$翅片= memory_get_usage();
呼应'< P>数组:'(microtime中(真) - $ T0)< / P>中;
呼应'< P>内存:'($装订$ INICIO)< / P>中;
P($ Z);$ T0 = microtime中(真);
$ arraysOf =阵列();
$ INICIO = memory_get_usage();
为($ I = 0; $ I< 1000; $ I ++){
$ Z =新stdClass的();
为($ J = 0; $ J< 1000; $ J ++){
$ Z-GT&; AAA ='AAA';
$ Z-GT&; BBB ='BBB';
$ Z-GT&; CCC = $ Z-GT&; $ z轴&GT AAA,BBB。
}
$ arraysOf [] = $ Z者除外;
}
$翅片= memory_get_usage();
呼应'< P>数组:'(microtime中(真) - $ T0)< / P>中;
呼应'< P>内存:'($装订$ INICIO)< / P>中;
P($ Z);
?>
和我得到的一个结果:
数组:1.8451430797577内存:460416排列
(
[AAA] => AAA
[BBB] => BBB
[CCC] => AAABBB
)数组:1.8294548988342内存:275696SomeClass的对象
(
[AAA] => AAA
[BBB] => BBB
[CCC] => AAABBB
)数组:2.2577090263367内存:483648stdClass的对象
(
[AAA] => AAA
[BBB] => BBB
[CCC] => AAABBB
)
结论PHP 5.4
- 类是斋戒比阵列(但轻微)。
- stdClass的是邪恶的。
- 类使用比数组的内存更少。 (约30-40%少!)
PS:作为一个音符,如果类定义但随后的成员,使用这个类的比较慢。它还使用更多的内存。 显然,秘密在于定义成员
更新
我从PHP 5.4升级到PHP 5.5(5.5.12 86窗口)。
数组:1.6465699672699内存:460400排列
(
[AAA] => AAA
[BBB] => BBB
[CCC] => AAABBB
)数组:1.8687851428986内存:363704SplFixedArray对象
(
[0] => AAA
[1] => BBB
[2] => AAABBB
)数组:1.8554251194内存:275568SomeClass的对象
(
[AAA] => AAA
[BBB] => BBB
[CCC] => AAABBB
)数组:2.0101680755615内存:483656stdClass的对象
(
[AAA] => AAA
[BBB] => BBB
[CCC] => AAABBB
)
结论PHP 5.5
- 对于数组,PHP 5.5比5.4的PHP更快,对象是pretty大同小异
- 类比阵列由于PHP 5.5和阵列的优化速度较慢。
- stdClass的是邪恶的。
- 类仍然使用比数组的内存更少。 (减少约30%-40%!)。
- SplFixedArray类似于使用类,但它使用更多的内存。
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?
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
?
Edit: 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
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);
?>
And i obtained the next result:
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
)
Conclusion for php 5.4
- Class is fasts than Arrays (but marginally).
- stdClass is evil.
- Class uses less memory than Arrays. (about 30-40% less!!)
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
Update
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
)
Conclusion for php 5.5
- For arrays, PHP 5.5 is faster than PHP 5.4, for object it is pretty much the same
- Class is slower than Arrays thanks to the optimization of PHP 5.5 and arrays.
- stdClass is evil.
- Class still uses less memory than Arrays. (about 30-40% less!!).
- SplFixedArray is similar to use a Class but it uses more memory.
这篇关于PHP对象VS阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!