Possible Duplicate:
When should I use stdClass and when should I use an array in php5 oo code ??
What are the benefits of using one of the two structures over the other?
// array $user['name'] = 'Emanuil'; // object $user->name = 'Emanuil';
Both objects and arrays are considered “special” in JavaScript. Objects represent a special data type that is mutable and can be used to store a collection of data (rather than just a single value). Arrays are a special type of variable that is also mutable and can also be used to store a list of values.
The short version: Arrays are mostly faster than objects.
Array. filter() removes all duplicate objects by checking if the previously mapped id-array includes the current id ( {id} destructs the object into only its id). To only filter out actual duplicates, it is using Array.
At 10k elements, both tests ran comparable times (array: 16.6 ms, set: 20.7 ms) but when dealing with 100k elements, the set was the clear winner (array: 1974.8 ms, set: 83.6 ms) but only because of the removing operation. Otherwise the array was faster.
Arrays
array_*
functions that can work on arrays, most of which are very fast.Objects
__get
, __set
, etc)Just run a simple test:
$ts_o = microtime(true); for($i=0;$i<=1000;$i++) { new stdClass(); } $total_object = microtime(true) - $ts_o;
Versus:
$ts_a = microtime(true); for($i=0;$i<=1000;$i++) { array(); } $total_array = microtime(true) - $ts_a;
And calculate the he results.
echo 'Object: ' . $total_object . ' / Array: ' . $total_array;
Results: Object: 0.002635 / Array: 0.001243
As you can see that Arrays are faster in regards to speed, average 46.6% infact.
But when you start adding variables they suddenly turn around:
$ts_o = microtime(true); for($i=0;$i<=1000;$i++) { $var = new stdClass(); $var->booleon = true; } $total_object = microtime(true) - $ts_o; unset($var); $ts_a = microtime(true); for($i=0;$i<=1000;$i++) { $var = array(); $var['booleon'] = true; } $total_array = microtime(true) - $ts_a; echo 'Object: ' . ($total_object) . ' / Array: ' . $total_array;
New Results: 0.0037809 / Array: 0.0046189
There's a few test you would have to do then find your mean / mode at the end of the test to find the one that truly is the better entity.
You can do a test on memory by doing a memory_get_usage
: http://php.net/manual/en/function.memory-get-usage.php with the same principles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With