![arraysAreFast](https://preview.redd.it/iyof1qxb7a9d1.jpeg?auto=webp&s=a4cde1ec4d69425314490c0841cd0fa79cd07470)
View all comments
What is the difference?
They produce the same result, but the one at the top keeps "jumping around" in memory, resulting in significantly worse performance.
Its quite possible that a modern compiler would produce the same code for both, however.
I felt this made sense at the beginning, but then I started wondering... isn't the purpose of the "random access" in RAM exactly that? Will there really be a difference? Or is a difference created due to something else like caching a contiguous memory block?
This will be a caching thing. Caches tend to expect you to access adjacent memory blocks, so jumping around constantly makes the cache useless