Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline function pointer to avoid if statement

In my jpg decoder I have a loop with an if statement that will always be true or always be false depending on the image. I could make two separate functions to avoid the if statement but I was wondering out of curiosity what the effect on efficiency would be using a function pointer instead of the if statement. It will point to the inline function if true or point to an empty inline function if false.

class jpg{
  private:
    // emtpy function
    void inline nothing();
    // real function
    void inline function();
    // pointer to inline function
    void (jpg::*functionptr)() = nullptr;
}

jpg::nothing(){}

main(){

  functionptr = &jpg::nothing;
  if(trueorfalse){
    functionptr = &jpg::function;
  }

  while(kazillion){

    (this->*functionptr)();

    dootherstuff();

  }
}

Could this be faster than an if statement? My guess is no, because the inline will be useless since the compiler won't know which function to inline at compile time and the function pointer address resolve is slower than an if statement.

I have profiled my program and while I expected a noticeable difference one way or the other when I ran my program... I did not experience a noticeable difference. So I'm just wondering out of curiosity.

like image 352
deanresin Avatar asked Apr 07 '14 03:04

deanresin


People also ask

When inline method should not be used?

When we should avoid the use of inline? We should not use functions that are I/O bound as inline functions. When large code is used in some function, then we should avoid the inline. When recursion is used, inline function may not work properly.

What are disadvantages of inline?

5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. 6) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.

What is the main advantage of inline function?

Advantages of using Inline Functions No function call overhead occurs; hence enhanced program speed is obtained. It helps in saving the overhead of return call from a function. Helpful while calling a function in saving the overhead of variables push/pop on the stack.

What are the restriction of inline function?

Limitations of Inline FunctionsInline functions do not work if the body of the function contains any sort of looping or iteration. Inline functions do not support the use of switch or goto statements. C++ Inline functions cannot work if the function defined is recursive in nature.


2 Answers

It is very likely that the if statement would be faster than invoking a function, as the if will just be a short jump vs the overhead of a function call.

This has been discussed here: Which one is faster ? Function call or Conditional if Statement?

The "inline" keyword is just a hint to the compiler to tell it to try to put the instructions inline when assembling it. If you use a function pointer to an inline, the inline optimization cannot be used anyway:

Read: Do inline functions have addresses?

If you feel that the if statement is slowing it too much, you could eliminate it altogether by using separate while statements:

if (trueorfalse) {
    while (kazillion) {
        trueFunction();
        dootherstuff();
    }
} else {
    while (kazillion) {
        dootherstuff();
    }
}
like image 76
mbonneau Avatar answered Sep 20 '22 06:09

mbonneau


Caution 1: I am not really answering the above question, on purpose. If one wants to know what it faster between an if statement and a function call via a pointer in the above example, then mbonneau gives a very good answer.

Caution 2: The following is pseudo-code.

Besides curiosity, I truly think one should not ask himself what is faster between an if statement and a function call to optimize his code. The gain would certainly be very small, and the resulting code might be twisted in such a way it could impact readability AND maintenance.

For my research, I do care about performance, this is a fundamental notion I have to stick with. But I do more care about code maintenance, and if I have to choose between a good structure and a slight optimization, I definitely choose the good structure. Then, if it was me, I would write the above code as follows (avoiding if statements), using composition through a Strategy Pattern.

class MyStrategy {
  public:
    virtual void MyFunction( Stuff& ) = 0;
};

class StrategyOne : public MyStrategy {
  public:
    void MyFunction( Stuff& ); // do something
};

class StrategyTwo : public MyStrategy {
  public:
    void MyFunction( Stuff &stuff ) { } // do nothing, and if you 
                                        // change your mind it could
                                        // do something later.
};

class jpg{
  public:
    jpg( MyStrategy& strat) : strat(strat) { }
    void func( Stuff &stuff ) { return strat.MyFunction( stuff ); }
  private:
    ...
    MyStrategy strat;
}

main(){

  jpg a( new StrategyOne );
  jpg b( new StrategyTwo );
  vector<jpg> v { a, b };

  for( auto e : v )
  {
    e.func();

    dootherstuff();

  }
}
like image 41
Florian Richoux Avatar answered Sep 21 '22 06:09

Florian Richoux