DEV Community

Discussion on: Do they know it's C in PHP 🎄

out0 profile image

You should study data structure to get a better grasp of those kind of elements.

What languages provide in the standard lib (standard commands that they provide by default) are implementations of classical data structure algorithms.

Most of the modern languages end up implementing arrays using linked lists.

An array, in the traditional sense of the word, is a memory starting position and a length. So the index of the array is how much you increment the memory start position to get the next element.

So every array has to have a fixed length.

But languages can (and they do) encapsulate algorithms to make array expansion automatic, or they could even use linked lists, where each element of the array is comprised of at least 2 memory addresses: 1 for the data and 1 for the next element (or null)

So when you want to add something, you just come from the first element, then to the next, then to the next one... Until you reach the last one, than you can increment it by connecting it to the tail of that chain.

Variations of that algorithm use a little bit more memory in order to speed up add process...

To make the long story short, most of those data types they provide you will understand once you learn data structure.

Thread Thread
andersbjorkland profile image
Anders Björkland Author

Yes, I get that about arrays. I was rather thinking about arrays vs generators, as compared to compilation ahead of time vs just-in-time compilation. I don't know what languages you are used to but I believe that generators are not unique to PHP. I did a benchmark on a simple generator vs array the other day where a million-element long array with its printing took about 35.7MB of memory and the generator took 544 bytes. Quite the difference!!