1

I've recently picked up Michael Abrash's The Zen of Assembly Language (from 1990) and was reading a section on how instruction prefetching is not always advantageous such as the case when branching occurs (a jump). This is because all of the instructions that were prefetched are no longer the ones to be executed and so more instructions must be fetched.

This reminded me of an optimization from another old book, Tricks of the Game Programming Gurus by Andre LaMothe in which he suggests that when setting up your conditional statements, you put the most frequently (or expected) path first.

For example:

if (booleanThatIsMostLikelyToBeTrue)
{
   // ...expected code
   // also the code that would've been prefetched
}
else
{
   // ...exceptional or less likely code
}

My questions are:

1) Was LaMothe's optimization suggested with this in mind? (I no longer have the book)

2) Is this type of optimization still a worthwhile programming habit on modern machine? (maybe prefetching is handled differently than it used to be?)

wallyk
  • 56,922
  • 16
  • 83
  • 148
insominx
  • 338
  • 3
  • 14
  • Branch prediction still unfortunately affects modern programs in [**certain cases like this**](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array). Mostly, however, compiler optimizations take care of it. – Anirudh Ramanathan Oct 30 '12 at 21:00

2 Answers2

1

These kinds of optimizations can often be useful. However, it's typically something that you do after you've written the program, profiled it, and determined that the program would benefit from doing these micro-optimizations.

Also, many modern compilers have profile-guided optimization, which can relieve you of having to contort your code for performance purposes.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
1

You want to set up your code to branch as little as possible, and branch backwards when it does. A more reliable way to do that IF is to always do the common thing then test for the exception:

Do A; if( test ) Do B;

Of course, this has to be arranged so that anything A does is reversed by B if B occurs.

The point of Zen programming is to try to eliminate the If statements altogether. So for example, instead of looping 10 times (which requires an exit condition test), you just write the same statement 10 times, voila!, no if statement. Another example is if you are looping a list, you use a sentinel to exit the loop, instead of testing an index value.

If you are working in C, it can be difficult to gimick the compiler into doing what you want. Putting something first or second in an IF statement will have no effect on compiled result. Note that it is critical to use the right compiler options. For example, using the /O2 (optimize for speed) in Visual C++ makes a HUGE difference in the compiled efficiency.

Tyler Durden
  • 11,156
  • 9
  • 64
  • 126