Function pointer vs. IF/SWITCH speed

Started by
6 comments, last by Extrarius 19 years, 6 months ago
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?
Now I get a cookie!
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Excellent response. You, actually, were the person who made me curious about the subject.
Now I get a cookie!
Switch statements can also start looking ugly real fast.
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.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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).
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk



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

Even with no parameters...

Cant inline.
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...
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement