使用内联函数有什么问题?

时间:2022-04-30 15:32:24

While it would be very convenient to use inline functions at some situations,

虽然在某些情况下使用内联函数非常方便,

Are there any drawbacks with inline functions?

内联函数有什么缺点吗?

Conclusion:

结论:

Apparently, There is nothing wrong with using inline functions.

显然,使用内联函数没有什么错。

But it is worth noting the following points!

但值得注意的是以下几点!

  • Overuse of inlining can actually make programs slower. Depending on a function's size, inlining it can cause the code size to increase or decrease. Inlining a very small accessor function will usually decrease code size while inlining a very large function can dramatically increase code size. On modern processors smaller code usually runs faster due to better use of the instruction cache. - Google Guidelines

    过度使用内联实际上会使程序变慢。根据函数的大小,内联可以使代码大小增加或减少。内联一个非常小的访问函数通常会减少代码大小,而内联一个非常大的函数可以显著增加代码大小。在现代处理器中,由于更好地使用指令缓存,较小的代码通常运行得更快。——谷歌指南

  • The speed benefits of inline functions tend to diminish as the function grows in size. At some point the overhead of the function call becomes small compared to the execution of the function body, and the benefit is lost - Source

    内联函数的速度优势随着函数的增大而减小。在某些情况下,函数调用的开销与函数体的执行相比变得很小,而好处是丢失的。

  • There are few situations where an inline function may not work:

    有一些情况下,内联函数可能不能工作:

    • For a function returning values; if a return statement exists.
    • 对于返回值的函数;如果存在返回语句。
    • For a function not returning any values; if a loop, switch or goto statement exists.
    • 对于不返回任何值的函数;如果存在循环,则切换或goto语句。
    • If a function is recursive. -Source
    • 如果函数是递归的。源
  • The __inline keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not __inline is honored depends on the setting of the inline optimizer option. By default, the inline option is in effect whenever the optimizer is run. If you specify optimize , you must also specify the noinline option if you want the __inline keyword to be ignored. -Source

    __inline关键字只在指定优化选项时才会使函数内联。如果指定了优化,那么是否使用__inline取决于内联优化器选项的设置。默认情况下,无论何时运行优化器,inline选项都是有效的。如果要忽略__inline关键字,则还必须指定noinline选项。源

12 个解决方案

#1


21  

It worth pointing out that the inline keyword is actually just a hint to the compiler. The compiler may ignore the inline and simply generate code for the function someplace.

值得指出的是,inline关键字实际上只是给编译器的一个提示。编译器可能忽略内联,只为某个地方的函数生成代码。

The main drawback to inline functions is that it can increase the size of your executable (depending on the number of instantiations). This can be a problem on some platforms (eg. embedded systems), especially if the function itself is recursive.

内联函数的主要缺点是它可以增加可执行文件的大小(取决于实例化的数量)。这在某些平台上可能是个问题。特别是当函数本身是递归的时候。

I'd also recommend making inline'd functions very small - The speed benefits of inline functions tend to diminish as the function grows in size. At some point the overhead of the function call becomes small compared to the execution of the function body, and the benefit is lost.

我还建议将内联函数做得非常小—内联函数的速度优势随着函数的增大而减小。在某些时候,与执行函数体相比,函数调用的开销变得更小,从而失去了好处。

#2


14  

It could increase the size of the executable, and I don't think compilers will always actually make them inline even though you used the inline keyword. (Or is it the other way around, like what Vaibhav said?...)

它可以增加可执行文件的大小,而且我认为编译器不会总是使它们成为内联的,即使您使用内联关键字。(还是像Vaibhav说的那样?)

I think it's usually OK if the function has only 1 or 2 statements.

我认为如果函数只有1或2个语句通常是可以的。

Edit: Here's what the linux CodingStyle document says about it:

编辑:以下是linux代码格式文档的内容:

Chapter 15: The inline disease

第15章:内联疾病

There appears to be a common misperception that gcc has a magic "make me faster" speedup option called "inline". While the use of inlines can be appropriate (for example as a means of replacing macros, see Chapter 12), it very often is not. Abundant use of the inline keyword leads to a much bigger kernel, which in turn slows the system as a whole down, due to a bigger icache footprint for the CPU and simply because there is less memory available for the pagecache. Just think about it; a pagecache miss causes a disk seek, which easily takes 5 miliseconds. There are a LOT of cpu cycles that can go into these 5 miliseconds.

似乎存在一个常见的误解,即gcc有一个神奇的“让我更快”加速选项,称为“内联”。虽然inline的使用可能是合适的(例如,作为替换宏的一种方法,请参见第12章),但它通常不是合适的。大量使用inline关键字会导致更大的内核,这反过来会减慢系统的速度,因为CPU的icache占用更大,而pagecache可用的内存更少。想想它;pagecache缺失会导致磁盘查找,这很容易花费5毫秒。有很多cpu周期可以达到5毫秒。

A reasonable rule of thumb is to not put inline at functions that have more than 3 lines of code in them. An exception to this rule are the cases where a parameter is known to be a compiletime constant, and as a result of this constantness you know the compiler will be able to optimize most of your function away at compile time. For a good example of this later case, see the kmalloc() inline function.

一个合理的经验法则是不要将内联放在包含超过3行代码的函数中。这个规则的一个例外是,已知参数是compiletime常量,由于这个常量,编译器可以在编译时优化函数的大部分。对于后面的例子,请参见kmalloc()内联函数。

Often people argue that adding inline to functions that are static and used only once is always a win since there is no space tradeoff. While this is technically correct, gcc is capable of inlining these automatically without help, and the maintenance issue of removing the inline when a second user appears outweighs the potential value of the hint that tells gcc to do something it would have done anyway.

通常,人们认为向静态的且只使用一次的函数添加内联始终是一种胜利,因为没有空间权衡。虽然这在技术上是正确的,但是gcc可以在没有帮助的情况下自动内联,并且当第二个用户出现时,删除内联的维护问题超过了提示的潜在价值,提示告诉gcc去做它本来应该做的事情。

#3


4  

I agree with the other posts:

我同意其他员额:

  • inline may be superfluous because the compiler will do it
  • 内联可能是多余的,因为编译器会这么做
  • inline may bloat your code
  • 内联可能会使代码膨胀

A third point is it may force you to expose implementation details in your headers, .e.g.,

第三点是,它可能会强制您在头文件中公开实现细节。

class OtherObject;

class Object {
public:
    void someFunc(OtherObject& otherObj) {
        otherObj.doIt(); // Yikes requires OtherObj declaration!
    }
};

Without the inline a forward declaration of OtherObject was all you needed. With the inline your header needs the definition for OtherObject.

如果没有内联,那么您所需要的就是其他对象的前向声明。使用内联,您的头需要为OtherObject定义。

#4


4  

As others have mentioned, the inline keyword is only a hint to the compiler. In actual fact, most modern compilers will completely ignore this hint. The compiler has its own heuristics to decide whether to inline a function, and quite frankly doesn't want your advice, thank you very much.

正如其他人提到的,inline关键字只是对编译器的一个提示。实际上,大多数现代编译器将完全忽略这个提示。编译器有自己的启发式来决定是否内联一个函数,坦白地说,不需要您的建议,非常感谢。

If you really, really want to make something inline, if you've actually profiled it and looked at the disassembly to ensure that overriding the compiler heuristic actually makes sense, then it is possible:

如果你真的,真的想要做一些内联的东西,如果你已经对它进行了剖析并检查了反汇编以确保重写编译器启发式是有意义的,那么它是有可能的:

  • In VC++, use the __forceinline keyword
  • 在vc++中,使用__forceinline关键字
  • In GCC, use __attribute__((always_inline))
  • 在GCC,使用__attribute__((always_inline))

The inline keyword does have a second, valid purpose however - declaring functions in header files but not inside a class definition. The inline keyword is needed to tell the compiler not to generate multiple definitions of the function.

内联关键字确实有第二个、有效的目的——在头文件中声明函数,但不在类定义中。需要使用inline关键字来告诉编译器不要生成函数的多个定义。

#5


4  

There is a problem with inline - once you defined a function in a header file (which implies inline, either explicit or implicit by defining a body of a member function inside class) there is no simple way to change it without forcing your users to recompile (as opposed to relink). Often this causes problems, especially if the function in question is defined in a library and header is part of its interface.

有问题内联——一旦你定义一个函数在一个头文件(这意味着内联,显式或隐式定义的成员函数内部类),没有简单的方法来改变它没有迫使用户重新编译(而不是重新链接)。这通常会导致问题,特别是当问题函数在库中定义并且header是其接口的一部分时。

#6


2  

I doubt it. Even the compiler automatically inlines some functions for optimization.

我对此表示怀疑。甚至编译器也会自动为优化嵌入一些函数。

#7


2  

I don't know if my answer's related to the question but:

我不知道我的答案是否和这个问题有关,但是

Be very careful about inline virtual methods! Some buggy compilers (previous versions of Visual C++ for example) would generate inline code for virtual methods where the standard behaviour was to do nothing but go down the inheritance tree and call the appropriate method.

对于内联虚拟方法要非常小心!一些有bug的编译器(例如以前的Visual c++版本)将为虚拟方法生成内联代码,在这些虚拟方法中,标准的行为是什么都不做,而是沿着继承树调用适当的方法。

#8


1  

Inlining larger functions can make the program larger, resulting in more cache misses and making it slower.

内联较大的函数可以使程序更大,从而导致更多的缓存丢失,并使其更慢。

Deciding when a function is small enough that inlining will increase performance is quite tricky. Google's C++ Style Guide recommends only inlining functions of 10 lines or less.

决定一个函数何时足够小以至于内联将提高性能是相当棘手的。谷歌的c++风格指南建议只内联函数为10行或更少。

#9


1  

You should also note that the inline keyword is only a request. The compiler may choose not to inline it, likewise the compiler may choose to make a function inline that you did not define as inline if it thinks the speed/size tradeoff is worth it.

您还应该注意,inline关键字只是一个请求。编译器可能选择不内联,同样,如果编译器认为速度/大小的权衡值得,那么它也可能选择内联,使您没有将其定义为内联的函数。

This decision is generaly made based on a number of things, such as the setting between optimise for speed(avoids the function call) and optimise for size (inlining can cause code bloat, so isn't great for large repeatedly used functions).

这个决定通常是基于很多事情做出的,比如在优化速度(避免函数调用)和优化大小(内联会导致代码膨胀,所以对于大型重复使用的函数来说不是很好)之间进行设置。

with the VC++ compiler you can overide this decision by using __forceinline

使用vc++编译器,您可以使用__forceinline重写此决策

SO in general: Use inline if you really want to have a function in a header, but elsewhere theres little point because if your going to gain anything from it, a good compiler will be making it inline for you anyway.

一般来说,如果你想要在header中包含一个函数,可以使用inline;但在其他地方,这并没有什么意义,因为如果你想从中获得什么,一个好的编译器无论如何都会使它成为内联的。

#10


0  

Excessive inlining of functions can increase size of compiled executable which can have negative impact on cache performance, but nowadays compiler decide about function inlining on their own (depending on many criterias) and ignore inline keyword.

过多的函数内联会增加编译后的可执行文件的大小,这会对缓存性能产生负面影响,但是现在编译器会根据自己的情况决定函数内联(取决于许多标准),忽略内联关键字。

#11


0  

Among other issues with inline functions, which I've seen heavily overused (I've seen inline functions of 500 lines), what you have to be aware of are:

在内联函数的其他问题中,我已经看到了大量的过度使用(我见过500行内联函数),您需要注意的是:

  • build instability

    建立不稳定

    • Changing the source of an inline function causes all the users of the header to recompile
    • 更改内联函数的源代码将导致头的所有用户重新编译
    • #includes leak into the client. This can be very nasty if you rework an inlined function and remove a no-longer used header which some client has relied on.
    • #包括泄漏到客户端。如果您重新处理一个内联函数并删除某个客户所依赖的不再使用的header,那么这将非常糟糕。
  • executable size

    可执行文件的大小

    • Every time an inline is inlined instead of a call instruction the compiler has to generate the whole code of the inline. This is OK if the code of the function is short (one or two lines), not so good if the function is long
    • 每次内联而不是调用指令时,编译器必须生成内联的整个代码。如果函数的代码很短(一行或两行),这是可以的,如果函数很长,那就不太好了
    • Some functions can produce a lot more code than at first appears. I case in point is a 'trivial' destructor of a class that has a lot of non-pod member variables (or two or 3 member variables with rather messy destructors). A call has to be generated for each destructor.
    • 有些函数可以生成比最初更多的代码。我举的例子是类的一个“平凡的”析构函数,该类有许多非pod成员变量(或者两个或三个成员变量,具有相当杂乱的析构函数)。必须为每个析构函数生成一个调用。
  • execution time

    执行时间

    • this is very dependent on your CPU cache and shared libraries, but locality of reference is important. If the code you might be inlining happens to be held in cpu cache in one place, a number of clients can find the code an not suffer from a cache miss and the subsequent memory fetch (and worse, should it happen, a disk fetch). Sadly this is one of those cases where you really need to do performance analysis.
    • 这非常依赖于您的CPU缓存和共享库,但是引用的位置非常重要。如果您可能正在内联的代码碰巧在cpu缓存中保存在一个位置,那么许多客户端可以找到不受缓存丢失和后续内存获取的代码(更糟糕的是,如果发生磁盘获取)。遗憾的是,在这种情况下,您确实需要进行性能分析。

The coding standard where I work limit inline functions to simple setters/getters, and specifically say destructors should not be inline, unless you have performance measurements to show the inlining confers a noticeable advantage.

我工作的编码标准将内联函数限制为简单的setter /getter,并明确地说,析构函数不应该内联,除非您有性能度量,以显示内衬有明显的优势。

#12


-1  

  1. As other people said that inline function can create a problem if the the code is large.As each instruction is stored in a specific memory location ,so overloading of inline function make a code to take more time to get exicuted.

    正如其他人所说,如果代码很大,内联函数会产生问题。由于每个指令都存储在特定的内存位置中,所以对内联函数的重载使代码需要更多的时间才能得到exicuted。

  2. there are few other situations where inline may not work

    很少有其他情况下内联不能工作

    1. does not work in case of recursive function.
    2. 在递归函数的情况下不工作。
    3. It may also not work with static variable.
    4. 它也可能不能使用静态变量。
    5. it also not work in case there is use of a loop,switch etc.or we can say that with multiple statements.
    6. 在使用循环、开关等语句时,它也不起作用。
    7. And the function main cannot work as inline function.
    8. 函数main不能作为内联函数工作。

#1


21  

It worth pointing out that the inline keyword is actually just a hint to the compiler. The compiler may ignore the inline and simply generate code for the function someplace.

值得指出的是,inline关键字实际上只是给编译器的一个提示。编译器可能忽略内联,只为某个地方的函数生成代码。

The main drawback to inline functions is that it can increase the size of your executable (depending on the number of instantiations). This can be a problem on some platforms (eg. embedded systems), especially if the function itself is recursive.

内联函数的主要缺点是它可以增加可执行文件的大小(取决于实例化的数量)。这在某些平台上可能是个问题。特别是当函数本身是递归的时候。

I'd also recommend making inline'd functions very small - The speed benefits of inline functions tend to diminish as the function grows in size. At some point the overhead of the function call becomes small compared to the execution of the function body, and the benefit is lost.

我还建议将内联函数做得非常小—内联函数的速度优势随着函数的增大而减小。在某些时候,与执行函数体相比,函数调用的开销变得更小,从而失去了好处。

#2


14  

It could increase the size of the executable, and I don't think compilers will always actually make them inline even though you used the inline keyword. (Or is it the other way around, like what Vaibhav said?...)

它可以增加可执行文件的大小,而且我认为编译器不会总是使它们成为内联的,即使您使用内联关键字。(还是像Vaibhav说的那样?)

I think it's usually OK if the function has only 1 or 2 statements.

我认为如果函数只有1或2个语句通常是可以的。

Edit: Here's what the linux CodingStyle document says about it:

编辑:以下是linux代码格式文档的内容:

Chapter 15: The inline disease

第15章:内联疾病

There appears to be a common misperception that gcc has a magic "make me faster" speedup option called "inline". While the use of inlines can be appropriate (for example as a means of replacing macros, see Chapter 12), it very often is not. Abundant use of the inline keyword leads to a much bigger kernel, which in turn slows the system as a whole down, due to a bigger icache footprint for the CPU and simply because there is less memory available for the pagecache. Just think about it; a pagecache miss causes a disk seek, which easily takes 5 miliseconds. There are a LOT of cpu cycles that can go into these 5 miliseconds.

似乎存在一个常见的误解,即gcc有一个神奇的“让我更快”加速选项,称为“内联”。虽然inline的使用可能是合适的(例如,作为替换宏的一种方法,请参见第12章),但它通常不是合适的。大量使用inline关键字会导致更大的内核,这反过来会减慢系统的速度,因为CPU的icache占用更大,而pagecache可用的内存更少。想想它;pagecache缺失会导致磁盘查找,这很容易花费5毫秒。有很多cpu周期可以达到5毫秒。

A reasonable rule of thumb is to not put inline at functions that have more than 3 lines of code in them. An exception to this rule are the cases where a parameter is known to be a compiletime constant, and as a result of this constantness you know the compiler will be able to optimize most of your function away at compile time. For a good example of this later case, see the kmalloc() inline function.

一个合理的经验法则是不要将内联放在包含超过3行代码的函数中。这个规则的一个例外是,已知参数是compiletime常量,由于这个常量,编译器可以在编译时优化函数的大部分。对于后面的例子,请参见kmalloc()内联函数。

Often people argue that adding inline to functions that are static and used only once is always a win since there is no space tradeoff. While this is technically correct, gcc is capable of inlining these automatically without help, and the maintenance issue of removing the inline when a second user appears outweighs the potential value of the hint that tells gcc to do something it would have done anyway.

通常,人们认为向静态的且只使用一次的函数添加内联始终是一种胜利,因为没有空间权衡。虽然这在技术上是正确的,但是gcc可以在没有帮助的情况下自动内联,并且当第二个用户出现时,删除内联的维护问题超过了提示的潜在价值,提示告诉gcc去做它本来应该做的事情。

#3


4  

I agree with the other posts:

我同意其他员额:

  • inline may be superfluous because the compiler will do it
  • 内联可能是多余的,因为编译器会这么做
  • inline may bloat your code
  • 内联可能会使代码膨胀

A third point is it may force you to expose implementation details in your headers, .e.g.,

第三点是,它可能会强制您在头文件中公开实现细节。

class OtherObject;

class Object {
public:
    void someFunc(OtherObject& otherObj) {
        otherObj.doIt(); // Yikes requires OtherObj declaration!
    }
};

Without the inline a forward declaration of OtherObject was all you needed. With the inline your header needs the definition for OtherObject.

如果没有内联,那么您所需要的就是其他对象的前向声明。使用内联,您的头需要为OtherObject定义。

#4


4  

As others have mentioned, the inline keyword is only a hint to the compiler. In actual fact, most modern compilers will completely ignore this hint. The compiler has its own heuristics to decide whether to inline a function, and quite frankly doesn't want your advice, thank you very much.

正如其他人提到的,inline关键字只是对编译器的一个提示。实际上,大多数现代编译器将完全忽略这个提示。编译器有自己的启发式来决定是否内联一个函数,坦白地说,不需要您的建议,非常感谢。

If you really, really want to make something inline, if you've actually profiled it and looked at the disassembly to ensure that overriding the compiler heuristic actually makes sense, then it is possible:

如果你真的,真的想要做一些内联的东西,如果你已经对它进行了剖析并检查了反汇编以确保重写编译器启发式是有意义的,那么它是有可能的:

  • In VC++, use the __forceinline keyword
  • 在vc++中,使用__forceinline关键字
  • In GCC, use __attribute__((always_inline))
  • 在GCC,使用__attribute__((always_inline))

The inline keyword does have a second, valid purpose however - declaring functions in header files but not inside a class definition. The inline keyword is needed to tell the compiler not to generate multiple definitions of the function.

内联关键字确实有第二个、有效的目的——在头文件中声明函数,但不在类定义中。需要使用inline关键字来告诉编译器不要生成函数的多个定义。

#5


4  

There is a problem with inline - once you defined a function in a header file (which implies inline, either explicit or implicit by defining a body of a member function inside class) there is no simple way to change it without forcing your users to recompile (as opposed to relink). Often this causes problems, especially if the function in question is defined in a library and header is part of its interface.

有问题内联——一旦你定义一个函数在一个头文件(这意味着内联,显式或隐式定义的成员函数内部类),没有简单的方法来改变它没有迫使用户重新编译(而不是重新链接)。这通常会导致问题,特别是当问题函数在库中定义并且header是其接口的一部分时。

#6


2  

I doubt it. Even the compiler automatically inlines some functions for optimization.

我对此表示怀疑。甚至编译器也会自动为优化嵌入一些函数。

#7


2  

I don't know if my answer's related to the question but:

我不知道我的答案是否和这个问题有关,但是

Be very careful about inline virtual methods! Some buggy compilers (previous versions of Visual C++ for example) would generate inline code for virtual methods where the standard behaviour was to do nothing but go down the inheritance tree and call the appropriate method.

对于内联虚拟方法要非常小心!一些有bug的编译器(例如以前的Visual c++版本)将为虚拟方法生成内联代码,在这些虚拟方法中,标准的行为是什么都不做,而是沿着继承树调用适当的方法。

#8


1  

Inlining larger functions can make the program larger, resulting in more cache misses and making it slower.

内联较大的函数可以使程序更大,从而导致更多的缓存丢失,并使其更慢。

Deciding when a function is small enough that inlining will increase performance is quite tricky. Google's C++ Style Guide recommends only inlining functions of 10 lines or less.

决定一个函数何时足够小以至于内联将提高性能是相当棘手的。谷歌的c++风格指南建议只内联函数为10行或更少。

#9


1  

You should also note that the inline keyword is only a request. The compiler may choose not to inline it, likewise the compiler may choose to make a function inline that you did not define as inline if it thinks the speed/size tradeoff is worth it.

您还应该注意,inline关键字只是一个请求。编译器可能选择不内联,同样,如果编译器认为速度/大小的权衡值得,那么它也可能选择内联,使您没有将其定义为内联的函数。

This decision is generaly made based on a number of things, such as the setting between optimise for speed(avoids the function call) and optimise for size (inlining can cause code bloat, so isn't great for large repeatedly used functions).

这个决定通常是基于很多事情做出的,比如在优化速度(避免函数调用)和优化大小(内联会导致代码膨胀,所以对于大型重复使用的函数来说不是很好)之间进行设置。

with the VC++ compiler you can overide this decision by using __forceinline

使用vc++编译器,您可以使用__forceinline重写此决策

SO in general: Use inline if you really want to have a function in a header, but elsewhere theres little point because if your going to gain anything from it, a good compiler will be making it inline for you anyway.

一般来说,如果你想要在header中包含一个函数,可以使用inline;但在其他地方,这并没有什么意义,因为如果你想从中获得什么,一个好的编译器无论如何都会使它成为内联的。

#10


0  

Excessive inlining of functions can increase size of compiled executable which can have negative impact on cache performance, but nowadays compiler decide about function inlining on their own (depending on many criterias) and ignore inline keyword.

过多的函数内联会增加编译后的可执行文件的大小,这会对缓存性能产生负面影响,但是现在编译器会根据自己的情况决定函数内联(取决于许多标准),忽略内联关键字。

#11


0  

Among other issues with inline functions, which I've seen heavily overused (I've seen inline functions of 500 lines), what you have to be aware of are:

在内联函数的其他问题中,我已经看到了大量的过度使用(我见过500行内联函数),您需要注意的是:

  • build instability

    建立不稳定

    • Changing the source of an inline function causes all the users of the header to recompile
    • 更改内联函数的源代码将导致头的所有用户重新编译
    • #includes leak into the client. This can be very nasty if you rework an inlined function and remove a no-longer used header which some client has relied on.
    • #包括泄漏到客户端。如果您重新处理一个内联函数并删除某个客户所依赖的不再使用的header,那么这将非常糟糕。
  • executable size

    可执行文件的大小

    • Every time an inline is inlined instead of a call instruction the compiler has to generate the whole code of the inline. This is OK if the code of the function is short (one or two lines), not so good if the function is long
    • 每次内联而不是调用指令时,编译器必须生成内联的整个代码。如果函数的代码很短(一行或两行),这是可以的,如果函数很长,那就不太好了
    • Some functions can produce a lot more code than at first appears. I case in point is a 'trivial' destructor of a class that has a lot of non-pod member variables (or two or 3 member variables with rather messy destructors). A call has to be generated for each destructor.
    • 有些函数可以生成比最初更多的代码。我举的例子是类的一个“平凡的”析构函数,该类有许多非pod成员变量(或者两个或三个成员变量,具有相当杂乱的析构函数)。必须为每个析构函数生成一个调用。
  • execution time

    执行时间

    • this is very dependent on your CPU cache and shared libraries, but locality of reference is important. If the code you might be inlining happens to be held in cpu cache in one place, a number of clients can find the code an not suffer from a cache miss and the subsequent memory fetch (and worse, should it happen, a disk fetch). Sadly this is one of those cases where you really need to do performance analysis.
    • 这非常依赖于您的CPU缓存和共享库,但是引用的位置非常重要。如果您可能正在内联的代码碰巧在cpu缓存中保存在一个位置,那么许多客户端可以找到不受缓存丢失和后续内存获取的代码(更糟糕的是,如果发生磁盘获取)。遗憾的是,在这种情况下,您确实需要进行性能分析。

The coding standard where I work limit inline functions to simple setters/getters, and specifically say destructors should not be inline, unless you have performance measurements to show the inlining confers a noticeable advantage.

我工作的编码标准将内联函数限制为简单的setter /getter,并明确地说,析构函数不应该内联,除非您有性能度量,以显示内衬有明显的优势。

#12


-1  

  1. As other people said that inline function can create a problem if the the code is large.As each instruction is stored in a specific memory location ,so overloading of inline function make a code to take more time to get exicuted.

    正如其他人所说,如果代码很大,内联函数会产生问题。由于每个指令都存储在特定的内存位置中,所以对内联函数的重载使代码需要更多的时间才能得到exicuted。

  2. there are few other situations where inline may not work

    很少有其他情况下内联不能工作

    1. does not work in case of recursive function.
    2. 在递归函数的情况下不工作。
    3. It may also not work with static variable.
    4. 它也可能不能使用静态变量。
    5. it also not work in case there is use of a loop,switch etc.or we can say that with multiple statements.
    6. 在使用循环、开关等语句时,它也不起作用。
    7. And the function main cannot work as inline function.
    8. 函数main不能作为内联函数工作。