Posts in Category "Coding"

Coding Tip: Separate

I’ve been trying to put together a rather long list of coding tips to post here, so as to share what I’ve learned about coding for performance. I found it hard to put them all together and keep the post down to a few pages, so I’ve decided to post the tips over time. So, here goes my attempt at a first tip!

Separate your concepts – one operation per line. So instead of:

a[x] = b[x]*c[x]

do this:

temp1 = b[x]
temp2 = c[x]
temp3 = temp1 * temp2
a[x] = temp3

“Why?”, you might ask. Well, this simple code breaks down into 2 loads, one multiple, and one store. Putting all of it on one line might save you space in your .cpp file, but in an inner loop you will want to expand it as I have done for better performance.

The performance for this code in an inner loop is gained by hinting to the compiler and the CPU what is going on and what depends on what. Putting all of the loads up front allows it to possibly stream the loads better, and the same with the stores at the end. The compiler can look at this code and have an easier time figuring out how to unroll it. At run-time, it is possible that other instructions could be put in-between some of the operations to use the time wasted waiting for operations to complete (out of order execution). So, if your loads take too long and if it knows that the operations are independent of each other, it might be possible for it to do something else while waiting.

Well, that is my first tip. We’ll see how it goes as to whether I will do more programming tips or something else. I hope this helps.

-Edward