2

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.

jack_carver
  • 1,510
  • 2
  • 13
  • 28
  • 6
    Have you turned optimizations on? Are you in debug or release mode? – Xeo Apr 27 '11 at 05:14
  • 1
    When I turn the optimizer to -O3, the loop goes away completely. – Potatoswatter Apr 27 '11 at 05:17
  • @Xeo .. its just a single source file. I compile it like "g++ -finline-functions test.cpp", Either way there isn't any increase in performance.@ Potato just -O1 flag reduces to run time to 0.00 – jack_carver Apr 27 '11 at 05:18
  • It's strange, because the compiler should just substitute the code, as it does in the case of a macro; there shouldn't be such a major hit on performance – alex Apr 27 '11 at 05:18
  • 4
    @alex : It's not strange, because he's timing a debug build, and one should expect irrelevant/nonsensical timings when doing that. – ildjarn Apr 27 '11 at 05:36
  • 2
    After all, inline is merely a suggestion. Some compilers may ignore it for code generation. – Yakov Galka Apr 27 '11 at 06:03
  • 5
    Timing debug builds - there should be a law against that! – Bo Persson Apr 27 '11 at 06:09

2 Answers2

13

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.

Guy Sirton
  • 8,331
  • 2
  • 26
  • 36
10

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • As i said earlier. When I compile it using -O1 flag the runtime is 0.00 secs in both cases. I just want to why inlining is inefficient here. – jack_carver Apr 27 '11 at 05:22
  • 4
    @jack: If the time goes to *zero*, how are you drawing the conclusion that inlining is inefficient? – GManNickG Apr 27 '11 at 05:23
  • 1
    @jack: Maybe GCC simply ignores your `-finline-functions` flag when you're in debug mode and refuses to inline the functions? Don't know GCC enough, but sounds possible. – Xeo Apr 27 '11 at 05:24
  • ...and why is optimization important when it's not been enabled? you have a program which is more readable to a debugger... which seems desirable given the build settings... – justin Apr 27 '11 at 05:26
  • @Justin: I just wanted to know why g++ doesn't optimize such functions automatically. – jack_carver Apr 27 '11 at 05:30
  • Arguably the compiler should respect the inline keyword even if optimization is turned off. Optimization should control automatic inlining, not "requested" inlining... IMO. – Guy Sirton Apr 27 '11 at 05:32
  • 1
    @Guy Sirton : That would be a terrible thing for header-only libraries (such as the vast majority of [boost](http://www.boost.org/)'s libraries), which have to mark every function definition as inline to avoid violating the ODR. – ildjarn Apr 27 '11 at 05:38
  • 2
    @Jack: It's completely nonsensical to ask performance-related questions when you've compiled with optimizations turned off. To assume that the compiler will perform optimizations (like inlining functions) even when you've specifically requested that it *not* perform optimizations quite simply does not make any sense. – Cody Gray - on strike Apr 27 '11 at 05:42
  • @jack_carver fair enough. potatoswatter's edit (posted at the same time) appears to have answered that for you. – justin Apr 27 '11 at 05:46
  • @ildarn: You're right. So basically inline doesn't really mean inline. it means it's OK to have multiple definitions of the same function. I donno... :-) http://stackoverflow.com/questions/3647053/what-is-are-the-purposes-of-inline – Guy Sirton Apr 27 '11 at 06:24