什么是内联函数(inline function)

In C, we have used Macro function an optimized technique used by compiler to reduce the execution time etc. So Question comes in mind that what’s there in C++ for that and in what all better ways? Inline function is introduced which is an optimization technique used by the compilers especially to reduce the execution time. We will cover “what, why, when & how” of inline functions.

在C中,我们使用了宏函数,这是编译器使用的一种优化技术,可以减少执行时间等。所以问题是C ++中有什么内容以及更好的方式? 引入了内联函数,这是编译器专门用于减少执行时间的优化技术。 我们将介绍内联函数:“是什么,为什么,在什么时间和以什么方式”。

What is inline function :
The inline functions are a C++ enhancement feature to increase the execution time of a program. Functions can be instructed to compiler to make them inline so that compiler can replace those function definition wherever those are being called. Compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.
NOTE- This is just a suggestion to compiler to make the function inline, if function is big (in term of executable instruction etc) then, compiler can ignore the “inline” request and treat the function as normal function.

什么是内联函数:
内联函数是一种C++增强功能,可以增加程序的运行时间。 可以向编译器指示某些函数,使它们内联,以便编译器可以在调用它们的任何地方,替换那些函数定义。 编译器在编译时,替换内联函数的定义,而不是在运行时引用函数定义。
注意 - 这只是建议编译器使函数内联,如果函数很大(在可执行指令等方面),编译器可以忽略“内联”请求并将函数视为正常函数。

How to make function inline:
To make any function as inline, start its definitions with the keyword “inline”.

如何使函数内联:
要使任何函数成为内联函数,请使用关键字“inline”在其开始其定义的地方。

例子:

Class A
{
 Public:
    inline int add(int a, int b)
    {
       return (a + b);
    };
}

Class A
{
 Public:
    int add(int a, int b);
};

inline int A::add(int a, int b)
{
   return (a + b);
}

Why to use –
In many places we create the functions for small work/functionality which contain simple and less number of executable instruction. Imagine their calling overhead each time they are being called by callers.
When a normal function call instruction is encountered, the program stores the memory address of the instructions immediately following the function call statement, loads the function being called into the memory, copies argument values, jumps to the memory location of the called function, executes the function codes, stores the return value of the function, and then jumps back to the address of the instruction that was saved just before executing the called function. Too much run time overhead.
The C++ inline function provides an alternative. With inline keyword, the compiler replaces the function call statement with the function code itself (process called expansion) and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to another location to execute the function, and then jump back as the code of the called function is already available to the calling program.
With below pros, cons and performance analysis, you will be able to understand the “why” for inline keyword

为何使用 -
在许多地方,我们为小的工作/功能,创建了包含简单和少量可执行指令的函数。想象一下,每次调用时,他们的调用开销。
当遇到正常的函数调用指令时,程序会在函数调用语句之后立即存储指令的内存地址,将被调用的函数加载到内存中,复制参数值,跳转到被调用函数的内存位置,执行函数代码,存储函数的返回值,然后跳回到执行被调用函数之前保存的指令的地址。如果大量运行上述的小工作或功能时,会造成运行时间过多。
C ++内联函数提供了另一种选择。使用inline关键字,编译器将函数调用语句替换为函数代码本身(称为扩展的过程),然后编译整个代码。因此,使用内联函数,编译器不必跳转到另一个位置来执行该函数,然后跳回。因为被调用函数的代码已经可用于调用程序。
通过以下优点,缺点和性能分析,您将能够理解为什么需要内联关键字的“原因”

Pros :-
1. It speeds up your program by avoiding function calling overhead.
2. It save overhead of variables push/pop on the stack, when function calling happens.
3. It save overhead of return call from a function.
4. It increases locality of reference by utilizing instruction cache.
5. By marking it as inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining)

优点: -
1.它通过避免函数调用开销来加速你的程序。
2.当函数调用发生时,它可以节省堆栈上变量push / pop的开销。
3.它节省了函数返回调用的开销。
它通过利用指令缓存增加了引用的局部性。
5.通过将其标记为内联,您可以将函数定义放在头文件中(即它可以包含在多个编译单元中,而不会让链接器抱怨)

Cons :-
1. It increases the executable size due to code expansion.
2. C++ inlining is resolved at compile time. Which means if you change the code of the inlined function, you would need to recompile all the code using it to make sure it will be updated
3. When used in a header, it makes your header file larger with information which users don’t care.
4. As mentioned above it increases the executable size, which may cause thrashing in memory. More number of page fault bringing down your program performance.
5. Sometimes not useful for example in embedded system where large executable size is not preferred at all due to memory constraints.

缺点: -
1.由于代码扩展,它增加了可执行文件的大小。
2. C++内联在编译时解决。 这意味着如果更改内联函数的代码,则需要使用它重新编译所有代码以确保它将更新
3.在头文件中使用时,它会使您的头文件变大,而用户不关心这些信息。
4.如上所述,它增加了可执行文件的大小,这可能会导致内存崩溃。 更多的页面错误会降低程序性能。
5.有时在嵌入式系统中没有用,因为内存限制,根本不需要大的可执行文件大小。

When to use -
Function can be made as inline as per programmer need. Some useful recommendation are mentioned below-
1. Use inline function when performance is needed.
2. Use inline function over macros.
3. Prefer to use inline keyword outside the class with the function definition to hide implementation details.

何时使用 -
根据程序员的需要,可把函数变为内联函数。 下面是一些有用的建议 -
1.当需要性能时,使用内联函数。
2.可把宏替换为内联函数。
3.希望在类外部使用,带有函数定义的内联关键字,用来隐藏实现细节。

Key Points -
1. It’s just a suggestion not compulsion. Compiler may or may not inline the functions you marked as inline. It may also decide to inline functions not marked as inline at compilation or linking time.
2. Inline works like a copy/paste controlled by the compiler, which is quite different from a pre-processor macro: The macro will be forcibly inlined, will pollute all the namespaces and code, won't be easy to debug.
3. All the member function declared and defined within class are Inline by default. So no need to define explicitly.
4. Virtual methods are not supposed to be inlinable. Still, sometimes, when the compiler can know for sure the type of the object (i.e. the object was declared and constructed inside the same function body), even a virtual function will be inlined because the compiler knows exactly the type of the object.
5. Template methods/functions are not always inlined (their presence in an header will not make them automatically inline).
6. Most of the compiler would do in-lining for recursive functions but some compiler provides #pragmas-
microsoft c++ compiler - inline_recursion(on) and once can also control its limit with inline_depth.
In gcc, you can also pass this in from the command-line with --max-inline-insns-recursive

关键点 -
1.这只是一个建议而不是必须。编译器可能会也可能不会,内联您标记为内联的函数。它还可能决定内联,在编译或链接时未标记为内联的函数。
2.内联工作类似于编译器控制的复制/粘贴,这与预处理器宏完全不同:宏将被强制内联,将污染所有命名空间和代码,而且不易调试。
3.默认情况下,在类中声明和定义的所有成员函数都是Inline。所以不需要明确定义。
4.虚拟方法不应该是无法解决的。有时,当编译器可以确切地知道对象的类型(即,对象是在同一函数体内声明和构造)时,甚至虚拟函数也会被内联,因为编译器确切地知道对象的类型。
5.模板方法/功能并不总是内联的(它们在标题中的存在不会使它们自动内联)。
6.大多数编译器都会为递归函数做内联,但是有些编译器会提供#pragmas-microsoft c ++编译器--inline_recursion(on)和曾经也可以使用inline_depth控制其限制。
在gcc中,您还可以使用--max-inline-insns-recursive从命令行传入此内容

翻译自:http://www.cplusplus.com/articles/2LywvCM9/

posted @ 2019-01-21 20:44  立体风  阅读(11313)  评论(0编辑  收藏  举报