Why is inlining considered faster than a function call?

前端 未结 16 1560
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 04:04

Now, I know it\'s because there\'s not the overhead of calling a function, but is the overhead of calling a function really that heavy (and worth the bloat of having it inli

16条回答
  •  无人及你
    2020-12-13 04:29

    There is no calling and stack activity, which certainly saves a few CPU cycles. In modern CPU's, code locality also matters: doing a call can flush the instruction pipeline and force the CPU to wait for memory being fetched. This matters a lot in tight loops, since primary memory is quite a lot slower than modern CPU's.

    However, don't worry about inlining if your code is only being called a few times in your application. Worry, a lot, if it's being called millions of times while the user waits for answers!

提交回复
热议问题