<bdo id='d3QKw'></bdo><ul id='d3QKw'></ul>

    1. <tfoot id='d3QKw'></tfoot>
        <legend id='d3QKw'><style id='d3QKw'><dir id='d3QKw'><q id='d3QKw'></q></dir></style></legend>
      1. <small id='d3QKw'></small><noframes id='d3QKw'>

        <i id='d3QKw'><tr id='d3QKw'><dt id='d3QKw'><q id='d3QKw'><span id='d3QKw'><b id='d3QKw'><form id='d3QKw'><ins id='d3QKw'></ins><ul id='d3QKw'></ul><sub id='d3QKw'></sub></form><legend id='d3QKw'></legend><bdo id='d3QKw'><pre id='d3QKw'><center id='d3QKw'></center></pre></bdo></b><th id='d3QKw'></th></span></q></dt></tr></i><div id='d3QKw'><tfoot id='d3QKw'></tfoot><dl id='d3QKw'><fieldset id='d3QKw'></fieldset></dl></div>

      2. 为什么内联被认为比函数调用更快?

        Why is inlining considered faster than a function call?(为什么内联被认为比函数调用更快?)
            <tbody id='IaC8o'></tbody>

          <small id='IaC8o'></small><noframes id='IaC8o'>

          <legend id='IaC8o'><style id='IaC8o'><dir id='IaC8o'><q id='IaC8o'></q></dir></style></legend>

                <i id='IaC8o'><tr id='IaC8o'><dt id='IaC8o'><q id='IaC8o'><span id='IaC8o'><b id='IaC8o'><form id='IaC8o'><ins id='IaC8o'></ins><ul id='IaC8o'></ul><sub id='IaC8o'></sub></form><legend id='IaC8o'></legend><bdo id='IaC8o'><pre id='IaC8o'><center id='IaC8o'></center></pre></bdo></b><th id='IaC8o'></th></span></q></dt></tr></i><div id='IaC8o'><tfoot id='IaC8o'></tfoot><dl id='IaC8o'><fieldset id='IaC8o'></fieldset></dl></div>
                  <bdo id='IaC8o'></bdo><ul id='IaC8o'></ul>

                • <tfoot id='IaC8o'></tfoot>

                  本文介绍了为什么内联被认为比函数调用更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  现在,我知道这是因为没有调用函数的开销,但是调用函数的开销真的那么重(值得内联它的膨胀)?

                  Now, I know it's because there's not the overhead of calling a function, but is the overhead of calling a function really that heavy (and worth the bloat of having it inlined) ?

                  据我所知,当一个函数被调用时,比如说 f(x,y),x 和 y 被压入堆栈,堆栈指针跳转到一个空块,并开始执行.我知道这有点过于简单化了,但我是否遗漏了什么?几推一跳调用一个函数,真的有那么多开销吗?

                  From what I can remember, when a function is called, say f(x,y), x and y are pushed onto the stack, and the stack pointer jumps to an empty block, and begins execution. I know this is a bit of an oversimplification, but am I missing something? A few pushes and a jump to call a function, is there really that much overhead?

                  如果我忘记了什么,请告诉我,谢谢!

                  Let me know if I'm forgetting something, thanks!

                  推荐答案

                  除了没有调用(因此没有相关费用,例如调用前的参数准备和调用后的清理)之外,还有一个显着的优势内联.当函数体被内联时,它的体可以在调用者的特定上下文中重新解释.这可能会立即允许编译器进一步减少和优化代码.

                  Aside from the fact that there's no call (and therefore no associated expenses, like parameter preparation before the call and cleanup after the call), there's another significant advantage of inlining. When the function body is inlined, it's body can be re-interpreted in the specific context of the caller. This might immediately allow the compiler to further reduce and optimize the code.

                  举一个简单的例子,这个函数

                  For one simple example, this function

                  void foo(bool b) {
                    if (b) {
                      // something
                    }
                    else {
                      // something else
                    }
                  }
                  

                  如果作为非内联函数调用将需要实际分支

                  will require actual branching if called as a non-inlined function

                  foo(true);
                  ...
                  foo(false);
                  

                  但是,如果上述调用被内联,编译器将立即能够消除分支.本质上,在上述情况下,内联允许编译器将函数参数解释为编译时常量(如果参数是编译时常量)——这对于非内联函数通常是不可能的.

                  However, if the above calls are inlined, the compiler will immediately be able to eliminate the branching. Essentially, in the above case inlining allows the compiler to interpret the function argument as a compile-time constant (if the parameter is a compile-time constant) - something that is generally not possible with non-inlined functions.

                  然而,它甚至远不止于此.一般来说,内联启用的优化机会要深远得多.再举一个例子,当函数体被内联到特定调用者的上下文中时,编译器在一般情况下将能够将调用代码中存在的已知别名相关关系传播到内联函数代码中,从而可以更好地优化函数的代码.

                  However, it is not even remotely limited to that. In general, the optimization opportunities enabled of inlining are significantly more far-reaching. For another example, when the function body is inlined into the specific caller's context, the compiler in general case will be able to propagate the known aliasing-related relationships present in the calling code into the inlined function code, thus making it possible to optimize the function's code better.

                  同样,可能的例子很多,所有这些都源于这样一个基本事实,即内联调用沉浸在特定调用者的上下文中,从而实现了各种不可能实现的上下文间优化与非内联调用.通过内联,您基本上可以获得原始函数的许多单独版本,每个版本都针对每个特定的调用者上下文单独定制和优化.显然,这样做的代价是代码膨胀的潜在危险,但如果使用得当,它可以提供显着的性能优势.

                  Again, the possible examples are numerous, all of them stemming from the basic fact that inlined calls are immersed into the specific caller's context, thus enabling various inter-context optimizations, which would not be possible with non-inlined calles. With inlining you basically get many individual versions of your original function, each version is tailored and optimized individually for each specific caller context. The price of that is, obviously, the potential danger of code bloat, but if used correctly, it can provide noticeable performance benefits.

                  这篇关于为什么内联被认为比函数调用更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

                      <tfoot id='G7vfr'></tfoot>
                    1. <i id='G7vfr'><tr id='G7vfr'><dt id='G7vfr'><q id='G7vfr'><span id='G7vfr'><b id='G7vfr'><form id='G7vfr'><ins id='G7vfr'></ins><ul id='G7vfr'></ul><sub id='G7vfr'></sub></form><legend id='G7vfr'></legend><bdo id='G7vfr'><pre id='G7vfr'><center id='G7vfr'></center></pre></bdo></b><th id='G7vfr'></th></span></q></dt></tr></i><div id='G7vfr'><tfoot id='G7vfr'></tfoot><dl id='G7vfr'><fieldset id='G7vfr'></fieldset></dl></div>
                      <legend id='G7vfr'><style id='G7vfr'><dir id='G7vfr'><q id='G7vfr'></q></dir></style></legend>
                        <tbody id='G7vfr'></tbody>
                        <bdo id='G7vfr'></bdo><ul id='G7vfr'></ul>

                          <small id='G7vfr'></small><noframes id='G7vfr'>