Sunday, November 12, 2006

Nested loop performance improvement

Below is a nested loop that calculates values of d. To do that, a, b, and c are calculated in nested loops. Here is the nested loop that most of us would code:


Now, what would happen if we moved a and b out of the main loop and calculated them outside? The first thought that comes to mind is that performance would suffer since we increase the total number of operations:


However, the second scenario executes faster...as much as 5 times! That may mean the difference between life and death if you have execution time constraints. Another leaky abstraction at work...

mp3:
Cassius - Toop toop
Murat Boz - Aşkı bulamam ben
MFÖ - Sarı laleler
Randy Newman - Rider in the rain

No comments: