什么是内联函数(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/
什么是内联函数(inline function)的更多相关文章
- (转载)内联函数inline和宏定义
(转载)http://blog.csdn.net/chdhust/article/details/8036233 内联函数inline和宏定义 内联函数的优越性: 一:inline定义的类的内联函 ...
- 内联函数inline的用法
一.什么是内联函数 在C语言中,如果一些函数被频繁调用,不断地有函数入栈,即函数栈,会造成栈空间或栈内存的大量消耗.为了解决这个问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放 ...
- 【转】ios内联函数 inline
ios内联函数 inline 缘由 由于在学习使用UIScrollVew开发的过程中,碰到下面这个属性(设置内边距): @property(nonatomic) UIEdgeInsets scroll ...
- 07 c++中的内联函数inline
文章链接: 问题描述:类中成员函数缺省默认是内联的,如果在类定义时就在类内给出函数定义,那当然最好.如果在类中未给出成员函数定义,而又想内联该函数的话,那在类外要加上 inline,否则就认为不是内联 ...
- 《挑战30天C++入门极限》新手入门:关于C++中的内联函数(inline)
新手入门:关于C++中的内联函数(inline) 在c++中,为了解决一些频繁调用的小函数大量消耗栈空间或者是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数. 可能说到这里,很 ...
- iOS OC内联函数 inline的详解
inline 在iOS中的一些框架中,static inline是经常出现的关键字组合. static自不用多说,表示在当前文件中应用,如 static A, 在其它文件中也可以出现static A. ...
- 内联函数inline
1:使用inline函数的时候,必须使函数体和inline说明结合一起,否则编译器将视他为普通函数处理: false: inline void Coord::setcoord(int a,int b) ...
- 内联函数 inline
(一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...
- 从零开始学C++之从C到C++(二):引用、内联函数inline、四种类型转换运算符
一.引用 (1).引用是给一个变量起别名 定义引用的一般格式:类型 &引用名 = 变量名: 例如:int a=1; int &b=a;// b是a的别名,因此a和b是同一个单元 注 ...
- 内联函数 inline 漫谈
内联函数存在的结论是: 引入内联函数是为了解决函数调用效率的问题 由于函数之间的调用,会从一个内存地址调到另外一个内存地址,当函数调用完毕之后还会返回原来函数执行的地址.函数调用会有一定的时间开销,引 ...
随机推荐
- 图解Ajax工作原理
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6126542.html Ajax指Asynchronous JavaScript and XML(异步的 Jav ...
- java 文件复制
java实现文件复制 CreateTime--2017年9月7日15:04:48 Author:Marydon 1.需求 根据原文件复制一份到指定位置 2.代码实现 需要导入: import ja ...
- Java读取并下载网络文件
CreateTime--2017年8月21日10:11:07 Author:Marydon import java.io.ByteArrayOutputStream; import java.io ...
- CSDN开源夏令营 基于Compiz的switcher插件设计与实现之编译compiz源代码
在開始介绍之前先吐个嘈:上周我们暑期ACM集训開始了.平均下来基本上是一天一赛.有时还不止.又是多校联赛,又是CodeForces,又是TopCoder.又是BestCoder,又是AcDream.还 ...
- qt坐标系统与布局的简单入门
qt坐标系统 qt坐标系统比較简单 ); 上面的代码把button显示为父窗体的20,20处宽度为100,高度为100 接下去是布局 qt里面布局须要增加<QLayout.h>这个头 ...
- ArcGIS调整影像颜色输出
有碰到一些质量很差的遥感影像,颜色需要进行调整(主要是针对看)输出,这里记录一下ArcGIS中的调整输出方式. 1.首先把影像文件拖入ArcMap中,然后右键单击图层列表中的图像,选择属性. 2.选择 ...
- Electromagnetic radiation and Radio 电磁波/电磁辐射和无线电波
电磁辐射,又称电磁波,是由同相振荡且互相垂直的电场与磁场在空间中以波的形式传递能量和动量,其传播方向垂直于电场与磁场构成的平面. 电磁辐射的载体为光子,不需要依靠介质传播,在真空中的传播速度为光速.电 ...
- android官方资料
android develop – Guides http://developer.android.com/guide/ android develop – API Reference http:// ...
- ASP.NET HttpModule URL 重写 (一) 【Z】
大家好,又来和大家见面了,此次给大家带来的URL重写,关于URL重写是什么,有什么好处,如何重写,今天我和大家一起分享一下我的经验 一.URL重写 URL重写就是首先获得一个进入的URL请求然后把它重 ...
- ios实例开发精品文章推荐(8.12)11个处理触摸事件和多点触摸的JS库
11个处理触摸事件和多点触摸的JS库 触摸屏是现在所有智能手机的标配,还包括各种平板设备,而且很多桌面也慢慢在开始支持触摸操作.要开发支持触摸屏设备的Web应用,我们需要借助浏览器的触摸事件来实现. ...