How to make your code faster when you use IF THEN

I read this very cool article and I had to test it out for myself. I can say that for sure that if the pattern of your IF THEN statements are predictable, that you code will run faster. This is due to a concept called Branch Prediction that is located on the CPU. Basically, the digital circuit tries to guess which way a branch will go before it knows for sure. Therefore, if the prediction is correct, based on previous iterations, then the cycle will run faster.

I conducted a similar test which produced these result. Unfortunately, I do not have Visual Studio Premium or Ultimate so I could not chart the wrongly predicted branches. However, the timings I think prove the point well enough.

image

Having been a business major and studied marketing, sales, management, finance, etc… I always try to find the applicable usage for such things. However interesting I found this article, I was struck by this question: How can I take this knowledge and make my programs faster? Have I ever known in advance the pattern of data which will be processed in my program or if the users iteration would be processed in the same manner? Could I design a system in a way that promotes a pattern which the CPU could then predict and therefore get better performance? Yes, I can.

If you will be processing a lot of data and running through 1 to n IF statements, it would be a good idea to look at the structure of the data on the database and how you receive the data. I.e. if you are performing a select of the data from a database, perhaps format it in a way that the logic that will process it could run faster. If the data you will process comes in a flat file or XML file you may want to load it into memory, organize it so it is optimized to run in a predicable manner. You just have to look in the above graph to see that randomly organized data took almost 100 times longer than organized.

I most of my professional technical experience, challenges I have faced were less interesting and generally a more visible solution/issue was the cause. However, this cool and I like cool.

Download the source