It's claimed that array traversal is faster than linked list traversal. What would be the reasons for that?
Array elements being in cache, assuming the element size is small
enough to fit multiple elements in a cache line?
No dependent load delay from having to load the linked list next
pointer? If that was the case, then moving the load of the next
pointer to the beginning of the loop should ameliorate that somewhat.
Something else?
It's claimed that array traversal is faster than linked list traversal.
What would be the reasons for that?
Array elements being in cache, assuming the element size is small enough
to fit multiple elements in a cache line?
No dependent load delay from having to load the linked list next
pointer? If that was the case, then moving the load of the next pointer
to the beginning of the loop should ameliorate that somewhat.
Something else?
Joe Seigh
It's claimed that array traversal is faster than linked list
traversal. What would be the reasons for that?
The next element address in an array can be found by addition
The next element address in a linked list can be found by a load
With cache hits, add is 1 cycle
With cache hits, load is 3-4 cycles
jseigh <jseigh_es00@xemaps.com> wrote or quoted:
It's claimed that array traversal is faster than linked list
traversal. What would be the reasons for that?
Array traversal is hella faster than linked list traversal for a few
key reasons:
Cache Magic: Arrays stack elements right next to each other in
memory, so when you hit one element, the CPU cache scoops up the
neighbors too. It's like grabbing a whole burrito instead of just
one bean at a time.
Linked lists are all over the place, so you're constantly
reaching for new bites.
Pointer Drag: Linked lists are like following a treasure map -
you got to read the clue, then go to the spot. Arrays? It's more
like counting houses on Rodeo Drive - you know exactly where
everything is.
CPU Crystal Ball: Arrays are predictable, like traffic on the
405 at rush hour. The CPU can see it coming. Linked lists are
more like navigating Santa Monica on a holiday weekend - you
never know what's around the corner.
Speed Demon: Figuring out where to go next in an array is quick,
like zipping down PCH. Linked lists are more stop-and-go, like
cruising through Hollywood Boulevard.
Bottom line, arrays crush it for traversal 'cause they're all
about that efficient memory use, fewer pit stops, and a smooth
ride the CPU can really groove with.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 45:43:08 |
Calls: | 10,394 |
Calls today: | 2 |
Files: | 14,066 |
Messages: | 6,417,268 |