DEV Community

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

 
andersbjorkland profile image
Anders Björkland

Nothing is special, but incredibly fascinating!

I have not gone beyond the high level languages yet, but I remember a time when I thought Java's "write once, run anywhere" were unique among these languages. I've come to see it differently of late. Seeing a similar strategy as Java's being used in language like PHP (which I thought to be very different) just drives that point further. But then we have all this about JIT compiler and it's fast going beyond what I'm prepared to learn 😅 My surface level understanding of JIT compilers is like comparing them to how generators work - as in contrast to arrays. (arrays take up memory space equal to all its elements, while generators just have the potential for all the elements and return each as they're needed).

Thread Thread
 
out0 profile image
out0

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

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!!