Inline function v. Macro in C — What's the Overhead (Memory/Speed)?

后端 未结 9 1257
太阳男子
太阳男子 2020-12-02 13:18

I searched Stack Overflow for the pros/cons of function-like macros v. inline functions.

I found the following discussion: Pros and Cons of Different macro function

9条回答
  •  心在旅途
    2020-12-02 13:55

    As I understand it from some guys who write compilers, once you call a function from inside it is not very likely your code will be inlined anyway. But, that is why you should not use a macro. Macros remove information and leave the compiler with far fewer options to optimize. With multi-pass compilers and whole program optimizations they will know that inlining your code will cause a failed branch prediction or a cache miss or other black magic forces modern CPUs use to go fast. I think everyone is right to point out that the code above is not optimal anyway, so that is where the focus should be.

提交回复
热议问题