Benefits of inline functions in C++?

后端 未结 14 1361
终归单人心
终归单人心 2020-11-22 05:30

What is the advantages/disadvantages of using inline functions in C++? I see that it only increases performance for the code that the compiler outputs, but with today\'s opt

14条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 06:04

    Inlining is a suggestion to the compiler which it is free to ignore. It's ideal for small bits of code.

    If your function is inlined, it's basically inserted in the code where the function call is made to it, rather than actually calling a separate function. This can assist with speed as you don't have to do the actual call.

    It also assists CPUs with pipelining as they don't have to reload the pipeline with new instructions caused by a call.

    The only disadvantage is possible increased binary size but, as long as the functions are small, this won't matter too much.

    I tend to leave these sorts of decisions to the compilers nowadays (well, the smart ones anyway). The people who wrote them tend to have far more detailed knowledge of the underlying architectures.

提交回复
热议问题