What are the advantages of the inline function?

Asked 2 years ago, Updated 2 years ago, 23 views

What are the advantages/disadvantages of the inline function in C++? I heard it's a little faster. It might be faster on an old computer, These days, the compiler optimizes everything, the CPU is fast, and there's a lot of memory. Is it really fast to use online now?

c++ inline-functions

2022-09-22 13:57

1 Answers

The reason why the inline function is fast is because Theoretically, unlike normal functions, the compiler changes the invocation part of the inline function to the content of the function Because I don't push/pop on the call stack. (However, sometimes the compiler doesn't handle it inline, so the inline function is not always faster.)

As you said, it doesn't make a huge difference these days, but it's a little faster to use online if you do a lot of calculations as below.

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

for(int a = 0; a < 900000; ++a)
    for(int b = 0; b < 900000; ++b)
        aplusb_pow2(a, b);


2022-09-22 13:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.