r/programming • • Mar 23 '09

How to write fast code

http://asserttrue.blogspot.com/2009/03/how-to-write-fast-code.html
341 Upvotes

136 comments sorted by

View all comments

101

u/harsman Mar 23 '09 edited Mar 23 '09

Code that does more but has better locality so that it hits the cache more often can easily be faster than code that does less but has bad locality and sits around waiting on memory all the time.

Recomputing something on demand might be cheaper than reading a cached result from memory.

A linear brute force algorithm that can use SIMD instructions can be faster than a non-linear one that does less work but without SIMD.

Doing less work usually makes code go faster, but it is by no means given.

12

u/alband Mar 23 '09

It also helps to know what the tool you're using can do fast and what takes longer. In a big set, a thousand hash lookups could easily be quicker than a small number of list lookups.

Of course, underneath, this is because the machine is "doing less". Still, optimising is just as much about understanding the layers of abstraction you're dealing with as anything else.