PHP: Use associative arrays basically never
7 years ago in #php by crell (60)
$7.76
- Past Payouts $7.76
- - Author $5.92
- - Curators $1.84
34 votes
- lukestokes: $7.66
- marketstack: $0.02
- dick.sledge: $0.02
- lionindayard: $0.01
- uberbrady: $0.01
- crell: $0.00
- steem-engine: $0.00
- thsigit: $0.00
- vayuzhipsch: $0.00
- lersus77: $0.00
- maknah: $0.00
- shancarpe: $0.00
- annetlitvin: $0.00
- davletshi: $0.00
- shavgen111: $0.00
- blackberry777: $0.00
- dashakarapetyan: $0.00
- danielyana: $0.00
- halftimehydrated: $0.00
- artyrdavletov: $0.00
- and 14 more
I always start with arrays for quick prototyping then I jump back to objects for storing the same data. Not only because I suspected it would be faster (because of the class definition) but because the data I'm sharing with has their own methods that knows how to deal with that data. Here is an example that I moved array structures into their own class, the code is much nicer and it runs a bit faster if you measure a few million times.
Interesting article that confirms my theory :D, thanks for writing it.
Nice! Yeah, the ability to encapsulate behavior is one of the most obvious benefits of a class but there's been a general belief in PHP for years that doing so was more expensive than doing it "manually". That may have been true once, but it's definitely not true today. In fact quite the opposite.
An addendum, as a few people have pointed out to me on Twitter:
This applies to runtime behavior. PHP has another optimization where, if you define an array as a
constit gets placed in shared memory with the code, so the net memory cost to each process using that array is 0.That's really only applicable if:
In that case, a
constbig nested array may indeed be better both for CPU and memory.The runtime builder for that compiled code, though, is still better off using objects for memory efficiency so that you can produce that compiled code.
As always, context matters. :-)
Great write up, Larry. I won’t fight you. You made a good argument.
Oh good. We have enough things to fight about. I'd hate to add programming optimization to the list. :-)
Nice benchmark !
And what about extending
Serializableon the named class to still store it as an associative array ?Is it the best win-win combo ? Of course we need to ask if defining serialization for simple data struct is relevant 😊.
My guess is it would be slower because it has to call serialize/deserialize in user-space for each class. It might end up being smaller but the performance cost is likely not worth it. That said, I haven't tried.
How about using the array_multisort() instead of usort():
array_multisort( $list, array_column($list,'a'), array_column($list, 'b') );Running on a mac book pro, 2.2GHz Intel Core i7, 16GB, listing Av. of 3 runs:
Associative array (Sorting)
Object with public properties (sorting):
a tradeoff between memory and runtime ...
Interesting observation! If you're sorting an array, yes, that would make a big difference. However, the purpose of
usort()here was to provide a direct comparison between objects and arrays, so they had to be used in the same way. That meantusort()so that we could compare the property access in each. I didn't as much care about the sorting itself as sorting was an easy way to call$array['a']and$object->aa few zillion times. :-)This is rather older, but here's a post from Nikita Popov explaining the difference in storage in PHP 5.4:
The structs have changed dramatically in PHP 7, but the basic optimization he describes is still with us, and is the reason for these results.
Some more recent posts on the topic, too:
https://nikic.github.io/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html
https://nikic.github.io/2014/12/22/PHPs-new-hashtable-implementation.html
Hi there:
I really tried to use model classes but it is impractical.
Let's say we want to json_serialize. Ok, it is not a problem. But what if we have a field that it's composed by another model
class Customer { var $fieldId=1; var $name=""; var $typeCustomer=new TypeCustomer(); // or we could initialize in the constructor. }Serializing it's not fun. However, de-serializing (json) is a big challenge because the system doesn't understand the field $typeCustomer if an object and it de-serialize as stdClass, then every method attached to TypeCustomer fails.
https://dev.to/jorgecc/php-is-bad-for-object-oriented-programming-oop-282a
Very good post. I often had these issues with associative arrays while writung the code for websites like https://www.receivesms.co