jack_carver
jack_carver

Reputation: 1526

Inline function performance

I am having a slight performance issue with inline function. Consider the following code:-

inline int left(int x) {
    return 2*x;
}
inline int right(int x) {
    return 2*x+1;
}

main() {
    for(int i=0;i<200000000;i++) {
        int L=left(i);
        int R=right(i);
    }
}

This when compiled and executed (with and without -finline-function flag) takes around 1.90 secs on my computer. However when I replace the inline functions with macros it takes just 1.26 secs. Although its highly unlikely that a function will be executed 200 million times but still I would like to know if there anyway to achieve that kind to performance using inline functions ?

EDIT: After receiving a couple of offensive comments I realised I wasn't very clear with my question. I just wanted to know how to achieve the same performance without using any optimisation flags. Ofcourse its more sensible to simply use -O2 or -O3, I just wanted to learn.

Upvotes: 2

Views: 2246

Answers (2)

Potatoswatter
Potatoswatter

Reputation: 137830

For me, with GCC 4.2.1 at -O3 optimization, there is no difference in runtime between an inline function and a macro. It is 0.185 sec in either case, and I seriously doubt my laptop is 10x faster than your machine.

Running g++ -S further reveals that the object code is identical.

I did adjust int L and int R to volatile int to force it to actually execute the loop.

Edit: The purpose of lower optimization settings is to help debug. One reason the inline function might be slower at -O0 is that the compiler makes sure all the variables are in a coherent state that you can stop and look at in the debugger, at the line of code inside the inline function.

Sometimes optimization makes it impossible to break inside or step through an inline function, just like a macro.

Upvotes: 10

Guy Sirton
Guy Sirton

Reputation: 8401

Inlined functions and macros should be the same performance so your functions are probably not getting inlined. Try adding

__attribute__((always_inline))

to your function declaration. (Also see gcc docs here):

Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function even if no optimization level was specified.

Upvotes: 13

Related Questions