什么是內聯函數(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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM