<分区>
推荐在哪里使用其中之一?我想存储数据库中列出的文章中的数据。
这是一个简单的问题:
echo $Datastore->name; //I like it works with foreach
//vs
echo $Datastore['name'];
哪个最好? stdClass
和array
获取元素的速度有区别吗?
<分区>
推荐在哪里使用其中之一?我想存储数据库中列出的文章中的数据。
这是一个简单的问题:
echo $Datastore->name; //I like it works with foreach
//vs
echo $Datastore['name'];
哪个最好? stdClass
和array
获取元素的速度有区别吗?
最佳答案
有一个类似的问题What is better stdClass or (object) array to store related data? with this answer
Based on small test (http://phpfiddle.org/lite/code/cz0-hyf) I can say that using "new stdClass()" is about 3 times slowlier than other options.
It is strange, but casting an array is done very efficiently compared to stdClass.
But this test meters only execution time. It does not meter memory.
P.S. I used phpFiddle only to share code. Test were done at my local PC.
In answer to another similar question你可以看到这个结论:
- 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 - stdClass 与数组。推荐哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24527618/