什么是内联函数(inlinefunction)

更新时间:2023-05-05 09:42:11 阅读: 评论:0

什么是内联函数(inlinefunction)
In C, we have ud Macro function an optimized technique ud 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 ud 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 increa the execution time of a program. Functions can be instructed to compiler to make them inline so that compiler can replace tho function definition wherever tho 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 exe
cutable 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 u –
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 itlf (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 increas 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 increas 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 ud in a header, it makes your header file larger with information which urs don’t care.
4. As mentioned above it increas the executable size, which may cau thrashing in memory. More number of page fault bringing down your program performance.
5. Sometimes not uful 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 u -
Function can be made as inline as per programmer need. Some uful recommendation are mentioned below-
1. U inline function when performance is needed.
2. U inline function over macros.
3. Prefer to u 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 suppod 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 becau the compiler knows exactly the type of the object.
5. Template methods/functions are not always inlined (their prence 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从命令⾏传⼊此内容
翻译⾃:/articles/2LywvCM9/

本文发布于:2023-05-05 09:42:11,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/530997.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:函数   内联   编译器   定义   代码   编译
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图