網站首頁 編程語言 正文
一般遞歸與尾遞歸
一般遞歸
def normal_recursion(n): if n == 1: return 1 else: return n + normal_recursion(n-1)
執行:
normal_recursion(5)
5 + normal_recursion(4)
5 + 4 + normal_recursion(3)
5 + 4 + 3 + normal_recursion(2)
5 + 4 + 3 + 2 + normal_recursion(1)
5 + 4 + 3 + 3
5 + 4 + 6
5 + 10
15
可以看到, 一般遞歸, 每一級遞歸都產生了新的局部變量, 必須創建新的調用棧, 隨著遞歸深度的增加, 創建的棧越來越多, 造成爆棧?
尾遞歸
尾遞歸基于函數的尾調用, 每一級調用直接返回遞歸函數更新調用棧, 沒有新局部變量的產生, 類似迭代的實現:
def tail_recursion(n, total=0): if n == 0: return total else: return tail_recursion(n-1, total+n)
執行:
tail_recursion(5, 0)
tail_recursion(4, 5)
tail_recursion(3, 9)
tail_recursion(2, 12)
tail_recursion(1, 14)
tail_recursion(0, 15)
15
可以看到, 尾遞歸每一級遞歸函數的調用變成"線性"的形式. 這時, 我們可以思考,?雖然尾遞歸調用也會創建新的棧, 但是我們可以優化使得尾遞歸的每一級調用共用一個棧!, 如此便可解決爆棧和遞歸深度限制的問題!
C中尾遞歸的優化
gcc使用-O2
參數開啟尾遞歸優化:
int tail_recursion(int n, int total) { if (n == 0) { return total; } else { return tail_recursion(n-1, total+n); } } int main(void) { int total = 0, n = 4; tail_recursion(n, total); return 0; }
反匯編
$ gcc -S tail_recursion.c -o normal_recursion.S
$ gcc -S -O2 tail_recursion.c -o tail_recursion.S gcc開啟尾遞歸優化
對比反匯編代碼如下(AT&T語法, 左圖為優化后)
可以看到, 開啟尾遞歸優化前, 使用call調用函數, 創建了新的調用棧(LBB0_3); 而開啟尾遞歸優化后, 就沒有新的調用棧生成了, 而是直接pop bp指向的_tail_recursion函數的地址(pushq %rbp)然后返回, 仍舊用的是同一個調用棧!
Python開啟尾遞歸優化
cpython本身不支持尾遞歸優化, 但是一個牛人想出的解決辦法:實現一個 tail_call_optimized 裝飾器
#!/usr/bin/env python2.4 # This program shows off a python decorator( # which implements tail call optimization. It # does this by throwing an exception if it is # it's own grandparent, and catching such # exceptions to recall the stack. import sys class TailRecurseException: def __init__(self, args, kwargs): self.args = args self.kwargs = kwargs def tail_call_optimized(g): """ This function decorates a function with tail call optimization. It does this by throwing an exception if it is it's own grandparent, and catching such exceptions to fake the tail call optimization. This function fails if the decorated function recurses in a non-tail context. """ def func(*args, **kwargs): f = sys._getframe() if f.f_back and f.f_back.f_back \ and f.f_back.f_back.f_code == f.f_code: # 拋出異常 raise TailRecurseException(args, kwargs) else: while 1: try: return g(*args, **kwargs) except TailRecurseException, e: args = e.args kwargs = e.kwargs func.__doc__ = g.__doc__ return func @tail_call_optimized def factorial(n, acc=1): "calculate a factorial" if n == 0: return acc return factorial(n-1, n*acc) print factorial(10000)
這里解釋一下sys._getframe()
函數:
sys._getframe([depth]):Return a frame object from the call stack.
If optional integer depth is given, return the frame object that many calls below the top of the stack.
If that is deeper than the call stack, ValueEfror is raised. The default for depth is zero,
returning the frame at the top of the call stack.
即返回depth深度調用的棧幀對象.
import sys def get_cur_info(): print sys._getframe().f_code.co_filename # 當前文件名 print sys._getframe().f_code.co_name # 當前函數名 print sys._getframe().f_lineno # 當前行號 print sys._getframe().f_back # 調用者的幀
說一下tail_call_optimized實現尾遞歸優化的原理: 當遞歸函數被該裝飾器修飾后, 遞歸調用在裝飾器while循環內部進行, 每當產生新的遞歸調用棧幀時:?f.f_back.f_back.f_code == f.f_code:
, 就捕獲當前尾調用函數的參數, 并拋出異常, 從而銷毀遞歸棧并使用捕獲的參數手動調用遞歸函數. 所以遞歸的過程中始終只存在一個棧幀對象, 達到優化的目的.
為了更清晰的展示開啟尾遞歸優化前、后調用棧的變化和tail_call_optimized裝飾器拋異常退出遞歸調用棧的作用, 我這里利用pudb調試工具做了動圖:
開啟尾遞歸優化前的調用
開啟尾遞歸優化后(tail_call_optimized裝飾器)的調用
通過pudb右邊欄的stack, 可以很清晰的看到調用棧的變化.
因為實現了尾遞歸優化, 所以factorial(10000)都不害怕遞歸深度限制報錯啦!
原文鏈接:https://segmentfault.com/a/1190000007641519
相關推薦
- 2022-05-27 詳解Python實現字典合并的四種方法_python
- 2022-09-29 React函數組件useContext?useReducer自定義hooks_React
- 2022-10-06 react-router-dom入門使用教程(路由的模糊匹配與嚴格匹配)_React
- 2022-06-10 解決Qt設置QTextEdit行高的問題_C 語言
- 2022-09-01 Nginx?部署的虛擬主機使用?Let's?Encrypt?加密?https的方法_nginx
- 2022-12-06 Python基礎面向對象之繼承與派生詳解_python
- 2022-09-29 powershell與cmd的異同匯總_PowerShell
- 2023-03-15 Python多進程協作模擬實現流程_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支