C speed question

Moderator
Posts: 133
Joined: 2008.05
Post: #1
In a function that I will be calling a whole lot of times every frame, I need to know which of these options will be faster to do the following:

I have function pointers that return some information in a structure based on what function is in the function pointer(obviously). Sometimes it doesn't need to get any information at all, in which case the function pointers are NULL.

Now, option one(what I have temporarily have in place) is to check if the various function pointers are pointing a valid function address. This means I'm basically doing 2 if statements per frame per particle, so something along the lines of 18000 if statements, if you were really to push the limits.

Option two would mean creating a dummy function that returns 0 everytime, so I'm basically calling two functions that are just {return 0;}.

I guess to sum it up, does an if statement take more time than the function overhead to just return 0?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
the if statement would be quicker. Function call overhead is large.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #3
the reason for this is that a function call requires the contents of several registers to be pushed onto the stack (the contents of any registers you were using need to be saved), then later popped off the stack
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #4
Thanks. I was aware of the many register moves for the parameter and return address, but I didn't remember how costly they were. But I guess b address is a lot faster than n * mov register + b address, silly me.
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #5
If there's an if statement that needs to be really fast, and you're using GCC to compile (if you use XCode, it's probably compiling with GCC) you can use:
Code:
long __builtin_expect (long exp, long c)

to control the branch prediction, which can speed things up somewhat. For example:
Code:
#define unlikely(x) (__builtin_expect((x), 0))
...
(inside some loop)
  if( unlikely( x==5 ) ){
    numfives++;
  }
means that you want the branch prediction to predict that the code will not go into the if block, so you only pay the penalty of missing a branch prediction on the case when x actually is 5.

This trick won't usually buy you that much speed, but it's good to know if your code needs to be as fast as possible. Here's a reference I found for some gcc builtins.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #6
phydeaux: thanks for WICKED tip!! Grin I had no idea it was possible to give branch prediction hints
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #7
I thought it always predicts that it won't go into the block, since if a branch evaluates to false, it goes to the next line in assembly, which is faster than jumping somewhere else, so it assumes the first one.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #8
LongJumper Wrote:I thought it always predicts that it won't go into the block, since if a branch evaluates to false, it goes to the next line in assembly, which is faster than jumping somewhere else, so it assumes the first one.

It's a tad bit more complex:

http://www.extremetech.com/article2/0,15...321,00.asp

The development of branch prediction algorithms is a huge priority for chip companies like Intel and IBM. Generally, the better your branch prediction algorithms, the longer you can make your pipeline without worrying about it having a detrimental effect on your CPU's computational power.
Quote this message in a reply
Post Reply