当前位置:网站首页>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 .
边栏推荐
- How to solve the problem that easycvr does not display the interface when RTMP streaming is used?
- Read Amazon memorydb database based on redis
- Is it safe to invest in funds through daily funds? I intend to open an account to buy funds
- 数据库中数据的储存结构和方式是什么?
- #yyds干货盘点#尾递归比递归好在哪儿
- 最安全的现货白银的心理分析
- 打新债到底靠不靠谱呀?是不是安全的?
- 3dMax建模笔记(一):介绍3dMax和创建第一个模型Hello world
- DCC888 :SSA (static single assignment form)
- 百度交易中台之钱包系统架构浅析
猜你喜欢

Tidb monitoring upgrade: a long way to solve panic

Hierarchy selector

一文读懂基于Redis的Amazon MemoryDB数据库

I've been outsourcing for four years, but I feel it's useless

Typecho imitation of Lu Songsong's blog theme template / Technology Information blog theme template

cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧

How to refine permissions to buttons?

C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test
![[go] go array and slice (dynamic array)](/img/63/9a3fb70b202ca45828cd1b62897eec.jpg)
[go] go array and slice (dynamic array)

BGP federal comprehensive experiment
随机推荐
62. 不同路径
启牛学堂属于证券公司吗?开户安全吗?
cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧
I've been outsourcing for four years, but I feel it's useless
层次选择器
ROS1Noetic在Win11中安装记录
Is it safe to invest in funds through daily funds? I intend to open an account to buy funds
Typecho仿盧松松博客主題模板/科技資訊博客主題模板
SAP UI5 应用开发教程之一百零二 - SAP UI5 应用的打印(Print)功能实现详解
cadence SPB17.4 - 中文UI设置
數據庫中數據的儲存結構和方式是什麼?
Express, route, request object, response object, middleware, EJS template
graphite statsd接口数据格式说明
Notes on zhouguohua's reading
包管理工具--NPM、--CNPM、 --Yarn、 --CYarn
OpenCvSharp (C# OpenCV) 微信QRCode解码功能使用介绍(附源码)
华为云如何实现实时音视频全球低时延网络架构【上】
【首发】请求一下子太多了,数据库危
flowable 全局监听 监听流程的启动和流程的结束
Hierarchy selector