What is inlining?

前端 未结 10 1482
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 06:45

I am referring to this discussion. I have never written any code in C or in C++ . I do not have any CS background. However I have been working as Java developer for 5 years

相关标签:
10条回答
  • 2020-12-01 07:09

    When executing a given piece of code, whenever you call a standard function the execution time is slightly higher than dumping there the code contained into that function. Dumping every time the whole code contained in a function is on the other end unmainteinable because it obviously leads to a whole mess of duplication of code.

    Inlining solves the performance and maintainability issue by letting you declare the function as inline (at least in C++), so that when you call that function - instead of having your app jumping around at runtime - the code in the inline function is injected at compile time every time that given function is called.

    Downside of this is that - if you inline big functions which you call a lot of times - the size of your program may significantly increase (best practices suggest to do it only on small functions indeed).

    0 讨论(0)
  • 2020-12-01 07:09

    Basically, in C/C++, the compiler can inline functions, which means that rather than making a function call to do that operation, the code will be added to the calling function's block, so it will be as though it had never been a separate function call.

    This will go into more detail: http://www.codersource.net/cpp_tutorial_inline_functions.html

    0 讨论(0)
  • 2020-12-01 07:09

    The compiler optimization answers are correct. There is another usage, though - in refactoring, inlining refers to replacing a method call with the body of the method and then removing the method. See Inline Method. There are similar refactorings, such as Inline Class.

    EDIT: Note that refactoring is done manually or with a tool; in either case it involves changing the source code.

    0 讨论(0)
  • 2020-12-01 07:11

    Inline functions are used typically in C++ header files not Java. A C++ header file usually does not contain implemented code and is considered an interface to the cpp file of the same name, which does usually contain the implemented code. It is legal to include an inline function in a header file, usually a small lightweight function. Inline functions do come at a cost, so they should not be large memory-intensive operations. For small routines the performance hit is minimal and they are more used for convenience.

    0 讨论(0)
提交回复
热议问题