ios内联函数 inline

静态的内联函数:static inline

  • 引入内联函数是为了解决函数调用效率的问题

  • 由于函数之间的调用,会从一个内存地址调到另外一个内存地址,当函数调用完毕之后还会返回原来函数执行的地址。函数调用会有一定的时间开销,引入内联函数就是为了解决这一问题。

实践

那么引用内联函数到底有什么区别呢?万一面试问到了,那只能回答”为了解决函数调用效率的问题”?如果面试官再问“如何解决呢?”,那岂不是歇菜了!!不如自己写代码测试看看?!!打开xcode..

代码一

说明:定义一个add(int,int)函数并声明为static inline,并调用。

头文件:inline.h

<code class="hljs vala has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  inline.h</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  inline</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Created by fenglh on 15/8/24.</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Copyright (c) 2015年 fenglh. All rights reserved.</span>

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#ifndef inline_inline_h</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define inline_inline_h</span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> inline <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> a, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> b){
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> a+b;
}
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#endif</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>

.m文件:main.m

<code class="hljs vala has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  main.m</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  inline</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Created by fenglj on 15/8/24.</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Copyright (c) 2015年 fenglh. All rights reserved.</span>

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <Foundation/Foundation.h></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import "inline.h"</span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[]) {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> c = add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>);
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>

查看main.m的汇编文件,如下:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.section</span>    __TEXT,__text,regular,pure_instructions
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.globl</span>  _main
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.align</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x90</span>
<span class="hljs-label" style="box-sizing: border-box;">_main:</span>                                  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">## @main</span>
<span class="hljs-label" style="box-sizing: border-box;">Lfunc_begin0:</span>
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.loc</span>    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>                  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">## /Users/fenglihai/Desktop/inline/inline/main.m:14:0</span>
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cfi</span>_startproc
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">## BB#0:</span>
    pushq   %rbp
<span class="hljs-label" style="box-sizing: border-box;">Ltmp0:</span>
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cfi</span>_def_cfa_offset <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>
<span class="hljs-label" style="box-sizing: border-box;">Ltmp1:</span>
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cfi</span>_offset %rbp, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>
    movq    %rsp, %rbp
<span class="hljs-label" style="box-sizing: border-box;">Ltmp2:</span>
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cfi</span>_def_cfa_register %rbp
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">##DEBUG_VALUE: main:argc <- EDI</span>
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">##DEBUG_VALUE: main:argv <- RSI</span>
<span class="hljs-label" style="box-sizing: border-box;">Ltmp3:</span>
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">##DEBUG_VALUE: main:c <- 3</span>
    xorl    %eax, %eax
    <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.loc</span>    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> prologue_end     <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">## /Users/fenglihai/Desktop/inline/inline/main.m:17:5</span>
<span class="hljs-label" style="box-sizing: border-box;">Ltmp4:</span>
    popq    %rbp
    retq</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>

代码二

说明:定义一个add(int,int)函数并调用。

头文件:Header.h

<code class="hljs vala has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Header.h</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  notInline</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Created by fenglh on 15/8/25.</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Copyright (c) 2015年 fenglh. All rights reserved.</span>

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#ifndef notInline_Header_h</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define notInline_Header_h</span>

 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> a, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> b){
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> a+b;
}

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#endif</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

.m文件:main.m

<code class="hljs vala has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  main.m</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  notInline</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Created by fenglh on 15/8/25.</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//  Copyright (c) 2015年 fenglh. All rights reserved.</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span>

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <Foundation/Foundation.h></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import "Header.h"</span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[]) {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> c = add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>);
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

查看main.m的汇编文件,如下:

<code class="hljs perl has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">    .section    __TEXT,__text,regular,pure_instructions
    .globl  _add
    .align  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x90</span>
_add:                                   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## @add</span>
Lfunc_begin<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>:
    .file   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/fenglihai/Desktop/notInline/notInline"</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Header.h"</span>
    .loc    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>                  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## /Users/fenglihai/Desktop/notInline/notInline/Header.h:12:0</span>
    .cfi_startproc
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## BB#0:</span>
    pushq   <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>
Ltmp<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>:
    .cfi_def_cfa_offset <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>
Ltmp1:
    .cfi_offset <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>
    movq    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rsp</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>
Ltmp2:
    .cfi_def_cfa_register <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%edi</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>)
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%esi</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>)
    .loc    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">13</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> prologue_end     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## /Users/fenglihai/Desktop/notInline/notInline/Header.h:13:5</span>
Ltmp3:
    movl    -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>), <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%esi</span>
    addl    -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>), <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%esi</span>
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%esi</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%eax</span>
    popq    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>
    retq
Ltmp4:
Lfunc_end<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>:
    .cfi_endproc

    .globl  _main
    .align  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x90</span>
_main:                                  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## @main</span>
Lfunc_begin1:
    .loc    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>                  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## /Users/fenglihai/Desktop/notInline/notInline/main.m:12:0</span>
    .cfi_startproc
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## BB#0:</span>
    pushq   <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>
Ltmp5:
    .cfi_def_cfa_offset <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>
Ltmp6:
    .cfi_offset <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>
    movq    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rsp</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>
Ltmp7:
    .cfi_def_cfa_register <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>
    subq    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$3</span>2, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rsp</span>
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$1</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%eax</span>
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$2</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%ecx</span>
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$0</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>)
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%edi</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>)
    movq    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rsi</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>)
    .loc    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">13</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">13</span> prologue_end    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## /Users/fenglihai/Desktop/notInline/notInline/main.m:13:13</span>
Ltmp8:
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%eax</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%edi</span>
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%ecx</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%esi</span>
    callq   _add
    xorl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%ecx</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%ecx</span>
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%eax</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>)
    .loc    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>                  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">## /Users/fenglihai/Desktop/notInline/notInline/main.m:14:5</span>
    movl    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%ecx</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%eax</span>
    addq    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$3</span>2, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rsp</span>
    popq    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%rbp</span>
    retq</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li></ul>

上面2段代码可以看出,只有add函数的定义不一样,一个是加了static inline修饰,而另外一个没有。再对比一下汇编代码,发现的确有很大的不一样呀!!!给人第一感觉,有用static inline修饰的汇编之后的代码比没有static inline修饰的的汇编之后的代码简洁的多了!!

其次,在没有调用static inline修饰add函数的main.m汇编代码中,add函数是有单独的汇编代码的! 
ios内联函数 inline_第1张图片

而没有使用内联函数的main.m汇编代码中,仅仅只有main函数的汇编代码! 
ios内联函数 inline_第2张图片

再看看使用了内联函数的main.m汇编代码: 
ios内联函数 inline_第3张图片

对比两者的mian.m的汇编代码,可以发现,没有使用`static inline修饰的内联函数的mian函数汇编代码中,会出现 call 指令!这就是区别!调用call指令就是就需要: 
(1)将下一条指令的所在地址(即当时程序计数器PC的内容)入栈 
(2)并将子程序的起始地址送入PC(于是CPU的下一条指令就会转去执行子程序)。

恩恩,对于汇编就不扯淡了,凭借着上学时候学过点汇编只能深入到这里了!唉!那么得知,影响效率的原因就是在解决在call调用这里了!!

结论

1.使用inline修饰的函数,在编译的时候,会把代码直接嵌入调用代码中。就相当于用#define 宏定义来定义一个add 函数那样!与#define的区别是: 
1)#define定义的格式要有要求,而使用inline则就行平常写函数那样,只要加上`inline即可! 
2)使用#define宏定义的代码,编译器不会对其进行参数有效性检查,仅仅只是对符号表进行替换。 
3)#define宏定义的代码,其返回值不能被强制转换成可转换的适合的转换类型。可参考百度文科 关于inline

2.在inline加上`static修饰符,只是为了表明该函数只在该文件中可见!也就是说,在同一个工程中,就算在其他文件中也出现同名、同参数的函数也不会引起函数重复定义的错误!**

实践到这里,对于内联函数终的理解,终于加深理解和记忆了!!

你可能感兴趣的:(ios内联函数 inline)