当前位置:网站首页>Yyds dry goods counting tail recursion is better than recursion
Yyds dry goods counting tail recursion is better than recursion
2022-06-23 00:41:00 【51CTO】
Many programmers know that recursive calls , But I don't know what tail recursion is . Today, I will mainly analyze tail recursion .
recursive
Function call itself .
The conditions that constitute recursion :
- The sub problem must be the same thing as the original problem , And it's simpler ;
- You can't call itself unlimited , There must be an exit , Reduction to non recursive state processing .
Implementing factorials recursively :
working principle
Stack is also called stack , Store local variables of the program ( Static local variables are not included ,static The variable has a static area ). in addition to , When a function is called , The stack is used to pass parameters and return values . Due to the last in first out feature of the stack , So stacks are very convenient for storing / Resume call site . In this sense , We can think of the stack as a register 、 Memory area for exchanging temporary data .
When C When a function is called in the program , A space will be allocated in the stack to hold the information related to the call , Each call is considered active . The storage space on the stack is called active record or stack frame
Stack frame by 5 It's made up of regions : Input parameters 、 Return value space 、 Temporary storage space used to evaluate expressions 、 State information and output parameters saved during function call .
Stack is a great way to store function call information , However, the stack also has some shortcoming :
The stack maintains the information of each function call until the function returns , This takes up a considerable amount of space , Especially when many recursive calls are used in the program . besides , Because there is a lot of information that needs to be saved and restored , Therefore, it takes some time to generate and destroy active records .
In short , Recursive stack pressing and stack pushing , Both time and space are consumed .
To avoid consuming too much time and space , Next, we introduce tail recursion .
Tail recursion
When the recursive call is in the whole function body The last executed statement And its The return value is not part of the expression when , This recursive call is tail recursion .
Tail recursive function is characterized by no operation in the regression process , This feature is important , Because most modern compilers use this feature to automatically generate optimized code .
The factorial function is realized by tail recursion :
The implementation principle of tail recursion
When the compiler detects that a function call is tail recursive , It overwrites the current activity record instead of creating a new one on the stack . The compiler can do this , Because the recursive call is the last statement to be executed in the current active period , So when the call returns, there is nothing else to do in the stack frame , So there is no need to save the stack frame . By overwriting the current stack frame instead of adding a new one over it , In this way, the stack space used is greatly reduced , This makes the actual operation efficiency become higher .
边栏推荐
- “听觉”营销价值凸显,喜马拉雅迎来新局点
- Notes on zhouguohua's reading
- Kunlun distributed database sequence function and its implementation mechanism
- How to set the power-off auto start of easycvr hardware box
- 初学者如何快速入门深度学习?
- 【滑动窗口】leetcode992. Subarrays with K Different Integers
- 黄金etf持仓量如何算
- How to calculate the position of gold ETF
- What do you pay special attention to when you insert / update / delete / obtain millions of rows of data in a DML statement?
- LINQ 查询
猜你喜欢

华为云招募工业智能领域合作伙伴,强力扶持+商业变现

Psychological analysis of the safest spot Silver

黄金etf持仓量如何算

XML escape character cross reference table
![[machine learning watermelon book] update challenge [Day1]: 1.1 INTRODUCTION](/img/f6/b0df192502a59a32d8bac8c0862d02.png)
[machine learning watermelon book] update challenge [Day1]: 1.1 INTRODUCTION
![How Huawei cloud implements a global low delay network architecture for real-time audio and video [Part 1]](/img/70/dcb558cf265da9e396637c5b90cd1b.png)
How Huawei cloud implements a global low delay network architecture for real-time audio and video [Part 1]

SAP UI5 应用开发教程之一百零三 - 如何在 SAP UI5 应用中消费第三方库试读版

Analysis on the wallet system architecture of Baidu trading platform

2022天梯赛-全国总决赛复盘赛

Kunlun distributed database sequence function and its implementation mechanism
随机推荐
打新债开户安全么,怎么开
图神经网络有哪些用途和应用?
OSPF综合实验
How about precious metal spot silver?
Project directory navigation
中国国际期货有限公司怎么样,是正规的期货公司吗?网上开户安全吗?
Quelle est la structure et la façon dont les données sont stockées dans la base de données?
Kunlundb query optimization (II) project and filter push down
你踩过这些坑吗?谨慎在时间类型列上创建索引
【滑动窗口】leetcode992. Subarrays with K Different Integers
XML escape character cross reference table
Using geTx to build a more elegant structure of flutter pages
美团基于 Flink 的实时数仓平台建设新进展
Tidb monitoring upgrade: a long way to solve panic
LINQ 查询
黄金etf持仓量如何算
一文简述:钓鱼攻击知多少
Analysis on the wallet system architecture of Baidu trading platform
#yyds干货盘点# 解决剑指offer:把二叉树打印成多行
【GO】go mod模式, package 12import/add is not in GOROOT