Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Function pointers vs Switch

  • What is faster: Function pointers or switch?

The switch statement would have around 30 cases, consisting of enumarated unsigned ints from 0 to 30.

I could do the following:

class myType
{
    FunctionEnum func;
    string argv[123];
    int someOtherValue;
};
// In another file:
myType current;
// Iterate through a vector containing lots of myTypes
 // ... for ( i=0; i < myVecSize; i ++ )
    switch ( current.func )
    {
           case 1:
            //...
            break;
           // ........
           case 30:
             // blah
            break;
    }

And go trough the switch with func every time. The good thing about switch would also be that my code is more organized than with 30 functions.

Or I could do that (not so sure with that):

class myType
{
    myReturnType (*func)(int all, int of, int my, int args );
    string argv[123];
    int someOtherValue;
};

I'd have 30 different functions then, at the beginning a pointer to one of them is assigned to myType.

  • What is probably faster: Switch statement or function pointer?

Calls per second: Around 10 million. I can't just test it out - that would require me to rewrite the whole thing. Currently using switch.

I'm building an interpreter which I want to be faster than Python & Ruby - every clock cycle matters!

like image 344
Rawr Avatar asked Apr 18 '10 13:04

Rawr


People also ask

Should you use function pointers in C?

Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.

What is the advantage of function pointer in C?

In C, we can use function pointers to avoid code redundancy. For example a simple qsort() function can be used to sort arrays in ascending order or descending or by any other order in case of array of structures. Not only this, with function pointers and void pointers, it is possible to use qsort for any data type.

What is difference between function and function pointer in C?

Function Pointers are pointers(like variable pointers) which point to the address of a function. They actually calling the underlying function when you dereference them like a function call does. The main advantage of a function pointer is that you can pass it to another function as a parameter for example ...


2 Answers

Switch statements are typically implemented with a jump table. I think the assembly can go down to a single instruction, which would make it pretty fast.

The only way to be sure is to try it both ways. If you can't modify your existing code, why not just make a test app and try it there?

like image 66
Mark Ransom Avatar answered Sep 20 '22 15:09

Mark Ransom


I think the difference is negligible in most cases - your case might be an exception. Why don't you put together a simple prototype app to explicitly measure the performance of each solution? My guess is that it would not take more than an hour of work...

However, think about clarity and maintainability of the code as well. IMHO it is obvious that the function pointer solution wins hands down in this respect.

Update: Note also that even if one solution is, let's say, twice as fast as the other as it is, it still may not necessarily justify rewriting your code. You should profile your app first of all to determine how much of the execution time is actually spent in those switches. If it is 50% of total time, there is a reason to optimize it. If it is a couple of percents only, optimizing it would be a waste of effort.

like image 28
Péter Török Avatar answered Sep 18 '22 15:09

Péter Török