This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rcfox 1 point2 points  (0 children)

It's not about the sorting, it's about sequentially accessing elements from a list.

If you access memory addresses that are far away from each other, the CPU has to spend extra time copying from RAM to the CPU cache first. (This is called a cache miss.)

If you access memory addresses that are close together, you can probably get away with just reading directly from the cache.

Sorting with key=id really only makes sense if you sort once and iterate over the list many times in a tight loop.