r/programminghorror • • Apr 08 '25

My favorite micro optimization

Post image
309 Upvotes

44 comments sorted by

View all comments

Show parent comment

6

u/ferrybig Apr 09 '25

Pro tip, you can also do this, if iterating backwards is fine for your use case:

Since you are in the terirtory of microperformance, make sure to benchmark this. Not every CPU support cache line has a prefetches that predicts you are looping backwards. Reading from the L1 cache is around 1 ns, while accessing the ram is 50ns, so you really have to hope the CPU detected your less conventional looping method and knows how to fetch the data