What are some of the differences between using recursion to solve a problem versus using iteration?
2016-01-06 23:13
471 查看
The fact is that recursion is rarely the most efficient approach to solving a problem, and iteration is almost always more efficient. This is because there is usually more overhead associated with making recursive calls due to the fact that the call stack is
so heavily used during recursion (for a refresher on this, read here: Recursion tutorial).
This means that many computer programming languages will spend more time maintaining the call stack then they will actually performing the necessary calculations.
performance drawback. But, depending on the problem that you are trying to solve, that performance drawback can be very insignificant – in which case it makes sense to use recursion. With recursion, you also get the added benefit that other programmers can
more easily understand your code – which is always a good thing to have.
so heavily used during recursion (for a refresher on this, read here: Recursion tutorial).
This means that many computer programming languages will spend more time maintaining the call stack then they will actually performing the necessary calculations.
Does recursion use more memory than iteration?
Generally speaking, yes it does. This is because of the extensive use of the call stack.Should I use recursion or iteration?
Recursion is generally used because of the fact that it is simpler to implement, and it is usually more ‘elegant’ than iterative solutions. Remember that anything that’s done in recursion can also be done iteratively, but with recursion there is generally aperformance drawback. But, depending on the problem that you are trying to solve, that performance drawback can be very insignificant – in which case it makes sense to use recursion. With recursion, you also get the added benefit that other programmers can
more easily understand your code – which is always a good thing to have.
相关文章推荐
- BootStrap中关于input-group的问题(未解决)
- jQuery对象访问 jquery: "3.0.0-pre
- 史上最牛逼的纯CSS实现tab选项卡,闪瞎你的狗眼
- 前端开发入门:前端构建工具百度FIS
- JavaScript 操作 Cookie
- caffe blob
- javascript小白学习指南4--持续更新
- Nodejs webSocket test
- ReactJS 官网案例分析
- 剑指offer-替换空格
- javascript attachEvent addEventListener 监听事件
- bzoj1031【JSOI2007】字符加密Cipher
- JS_ 垃圾回收、变量与属性、作用域链
- JS模块化规范CommonJS,AMD,CMD
- 函数响应式编程—ReactiveCocoa 使用入门
- css3-选择器
- 什么是JS闭包
- Scala 并发编程之react和loop
- JavaScript打字游戏 01
- JavaScript函数惰性载入