Few questions about C++ inline functions

Viewed 457

The training materials from the class I took seem to be making two conflicting statements.

On one hand:

"Use of inline functions usually results in faster execution"

On the other hand:

"Use of inline functions may decrease performance due to more frequent swapping"

Question 1: Are both statements true?

Question 2: What is meant by "swapping" here?

Please glance at this snippet:

int powA(int a, int b) {
  return (a + b)*(a + b) ;
}

inline int powB(int a, int b) {
  return (a + b)*(a + b) ;
}

int main () {
    Timer *t = new Timer;

    for(int a = 0; a < 9000; ++a) {
        for(int b = 0; b < 9000; ++b) {
             int i = (a + b)*(a + b);       //              322 ms   <-----
            //  int i = powA(a, b);         // not inline : 450 ms
            //  int i = powB(a, b);         // inline :     469 ms
        }
    }

    double d = t->ms();
    cout << "-->  " << d << endl; 

    return 0;
}

Question 3: Why is performance so similar between powA and powB? I would have expected powB performance to be along 322ms, since it is, after all, inline.

6 Answers
Related