尾遞迴分析

來源:互聯網
上載者:User

昨天被問到了尾遞迴及編譯器對它的處理相關,一直對它沒有研究過,解釋得很含糊。

回來查了下,記錄如下:

遞迴有線性遞迴(普通的遞迴)和尾遞迴。
由於尾遞迴的特殊性,一般的編譯器會做些特殊處理。因此,在效率和開銷上,比普通遞迴好。
舉個例子,計算n!
1)線性遞迴:
type recurve(long n)
{
      return  (n == 1) ? 1 : n * recurve(n - 1); 
}
2)尾遞迴:
type recurve_tail(long n, long result)
{
          return (n == 1) ? result : recurve_tail(n - 1, result * n);   
}
再封裝成1)的形式:
type recurve(long n)
{
    return (n == 0) ? 1 : recurve_tail(n, 1);
}

分析:
很容易看出, 普通的線性遞迴比尾遞迴更加消耗資源。每次調用都使得調用鏈條不斷加長,系統不得不開闢新的棧進行資料儲存和恢複;而尾遞迴就
不存在這樣的問題, 因為他的狀態完全由n 和 a 儲存,並且,被調用函數返回的值即為要求的值,本函數再沒有作用,於是本函數不再儲存,直接在本函數堆棧上進行遞迴調用,
對於特殊情況,甚至可以不使用記憶體空間,直接在寄存器完成。

編譯器如何判斷是否尾遞迴?
返回的值是函數本身,沒有其它選擇。

看一下上述尾遞迴函式在gcc 4.3.2-1-1下未進行最佳化的編譯結果:

          .file  "rec.c"          .text  .globl recurve_tail          .type  recurve_tail, @function  recurve_tail:          pushl  %ebp          movl   %esp, %ebp          subl   $24, %esp          cmpl   $1, 8(%ebp)         je     .L2         movl   12(%ebp), %eax         movl   %eax, %edx         imull  8(%ebp), %edx         movl   8(%ebp), %eax         subl   $1, %eax         movl   %edx, 4(%esp)         movl   %eax, (%esp)         call   recurve_tail         movl   %eax, -4(%ebp)         jmp    .L3 .L2:         movl   12(%ebp), %eax         movl   %eax, -4(%ebp) .L3:         movl   -4(%ebp), %eax         leave         ret         .size  recurve_tail, .-recurve_tail         .ident "GCC: (Debian 4.3.2-1.1) 4.3.2"         .section       .note.GNU-stack,"",@progbits未進行最佳化,與普通遞迴處理方式相同,新開闢了棧;再看-O3最佳化結果:          .file  "rec.c"          .text          .p2align 4,,15  .globl recurve_tail          .type  recurve_tail, @function  recurve_tail:          pushl  %ebp          movl   %esp, %ebp          movl   8(%ebp), %edx         movl   12(%ebp), %eax         cmpl   $1, %edx         je     .L2        .p2align 4,,7         .p2align 3 .L5:         imull  %edx, %eax         subl   $1, %edx         cmpl   $1, %edx         jne    .L5 .L2:         popl   %ebp         ret         .size  recurve_tail, .-recurve_tail         .ident "GCC: (Debian 4.3.2-1.1) 4.3.2"         .section       .note.GNU-stack,"",@progbits

*在makfile檔案開頭定義CFLAGS = -O2,這樣使用makefile編譯時間才會最佳化;gdb中查看函數地址命令為:info line【函數名】;查看某個函數的彙編指令為:disass 【函數名】

原文出處:http://blog.csdn.net/hilyoo/article/details/4445476

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.