Iterating without incurring the cost of IF statements
<p>My question is based on curiosity and not whether there is another approach to the problem or not. It is a strange/interesting question, so please read it with an open mind.</p>

<p>Let's assume there is a game loop that is being called every frame. The game loop in turn calls several functions through a myriad of <code>if</code> statements. For example, if the user has GUI to false then don't refresh the GUI otherwise call <code>RefreshGui()</code>. There are many other <code>if</code> statements in the loop and they call their respective functions if they are true. Some are <code>if/if-else.../else</code> which are more costly in the worst case. Even the functions that are called, if the <code>if</code> statement is true, have logic. If user wants raypicking on all objects call <code>FunctionA()</code>, if user wants raypicking on lights, call FunctionB(), ... , else call all functions. Hopefully you get the idea.</p>

<p>My point is, that is a lot of redundant if statements. So I decided to use function pointers instead. Now my assumption is that a function pointer is always going to be faster than an <code>if</code> statement. It is a replacement for if/else. So if the user wants to switch between two different camera modes, he/she presses the <code>C</code> key to toggle between them. The callback function for the keyboard changes the function pointer to the correct <code>UpdateCamera</code> function (in this case, the function pointer can point to either <code>UpdateCameraFps()</code> or <code>UpdateCameraArcBall()</code> )... you get the gist of it.</p>

<p>Now to the question itself. What if I have several update functions all with the same signature (let's say void <code>(*Update)(float time)</code> ), so that a function pointer can potentially point to any one of them. Then, I have a vector which is used to store the pointers. Then in my main update loop, I go through the vector and call each update function. I can remove/add and even change the order of the updates, without changing the underlying code. In the best case, I might only be calling one update function or in the worst case all of them, all with a very clean while loop and no nasty (potentially nested) if statements. I have implemented this part and it works great. I am aware, that, with each iteration of the while loop responsible for iterating through the vector, I am checking whether the <code>itrBegin == itrEnd</code>. More specifically <code>while (itrBegin != itrEnd)</code>. Is there any way to avoid the call to the if statements? Can I use branch prediction to my advantage (or am I taking advantage of it already without knowing)?</p>

<p>Again, please take the question as-is, i.e. I am not looking for a different approach (although you are more than welcome to give one).</p>

<blockquote>
  <p>EDIT: A few replies state that this is an unneeded premature optimization and I should not be focusing on it and that the if-statement(s) cost is minuscule compared to the work done in all the separate update functions. Very true, and I completely agree, but that was not the point of the question and I apologize if I did not make the question clearer. I did learn quite a few new things with all the replies though!</p>
</blockquote>
