Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using arrays VS objects for storing data [duplicate]

Tags:

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'; 
like image 640
Emanuil Rusev Avatar asked Sep 14 '10 14:09

Emanuil Rusev


People also ask

Why is it better to use objects instead of arrays?

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.

Are objects faster than arrays?

The short version: Arrays are mostly faster than objects.

How do you remove duplicates from array of 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.

Are sets more efficient than arrays?

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.


2 Answers

Arrays

  • There are tons of array_* functions that can work on arrays, most of which are very fast.
  • By default they passes by value (copied around)
  • Lightweight/Simple (changes only effect local variable, less to think about)
  • Often used for build once data (data that doesn't change)
  • All data is public
  • Slightly less resource intensive

Objects

  • Methods can be used to keep the data stricter. (IE. checks that a field fits a format)
  • Subclassing (reducing code duplication)
  • By default they are passed by reference
  • Changes to data can have cascading effects (__get, __set, etc)
  • Often used for data that is more mutable
  • Can protect data from outside via functions and protected/private variables
  • Function type hinting of objects is more flexible (different typehint for different classes)
like image 63
Kendall Hopkins Avatar answered Dec 18 '22 10:12

Kendall Hopkins


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.

like image 36
RobertPitt Avatar answered Dec 18 '22 10:12

RobertPitt