Jump to content
  • Advertisement
Sign in to follow this  
Axenation

Function pointer vs. IF/SWITCH speed

This topic is 5177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Will a function pointer ALWAYS be faster than an if/switch statement? Does the size and starting address of the dereferenced function affect speed? If so, then why don't people use function pointer arrays for handling opcodes in Virtual Machines rather than using huge switch statements?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Will a function pointer ALWAYS be faster than an if/switch statement?


Not if the function pointer is computed too soon before the jump, screwing up speculative execution, and producing a pipeline bubble.

You get branch prediction for conditional jumps.

Quote:
Does the size and starting address of the dereferenced function affect speed?


Depends on whether the function is in cache or not.

Quote:
If so, then why don't people use function pointer arrays for handling opcodes in Virtual Machines rather than using huge switch statements?


Who said they didn't? And compiler can turn (some) switch statements into jump tables - basically function pointer arrays.

Share this post


Link to post
Share on other sites
No, it wont always be faster.
When there is only a small number of cases, or the frequency distribution is very skewed such that the first item is the most frequently occurring, then a switch is probably faster.

In other cases a function pointer array is the best way to go because it provides constant time access, where the switch statement is (on virtually every compiler out there thus far) O(n). Switch statements are over used, as there is lots of code people write, for which a function pointer array (or other kind of array in some circumstances) would be so much more efficient.

If your cases are numbers like 1, 21, 321, 4321, 54321 then a function pointer array becomes a bit ridiculous with the wasted space, so you have to start adding hashing to your function pointer table, which again slows it down. Not to say that it won't beat a switch, but in those cases it probably is best to just stick to a switch for simplicity.

Share this post


Link to post
Share on other sites
A switch is often faster than a variable function pointer array, because it will turn into a 'hard coded jump table' such that the 'label pointer array' won't need to be loaded from memory etc. Also, taking the address of stuff more often can make compilers more cautious about optimzations because of the aliasing, such that some optimizations that can be applied might not get applied because of the aliasing.

A switch or a const function pointer array are probably best, except in rare situations where a binary tree made with nested ifs will be faster(usually a switch will be turned into either a const func ptr array or the binary tree, or a combo, depending on the values of the cases).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster



Call overhead ???? (versus switch doing table based jump)

Even with no parameters...

Cant inline.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
...
Yeah, the switch version gets converted to jumps rather than calls(aka no call overhead, but you still have the branch), and doesn't need to be inlined because it already is inline...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!