我应该避免 iPhone 上的递归吗?
我应该避免在 iPhone 上运行的代码出现递归吗?
或者换句话说,有人知道 iPhone 上的最大堆栈大小吗?
Should I avoid recursion with code that runs on the iPhone?
Or put another way, does anyone know the max stack size on the iphone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,避免递归在所有嵌入式平台上都是一件好事。
它不仅可以降低甚至消除堆栈溢出的可能性,而且通常还可以为您提供更快的代码。
您始终可以将递归算法重写为迭代算法。 但这并不总是实用(想想快速排序)。 解决这个问题的一种方法是以限制递归深度的方式重写算法。
Introsort 是实践中如何进行的完美示例。 它将快速排序的递归深度限制为 log2(元素数)。 因此,在 32 位计算机上,您的递归深度永远不会超过 32 位。
http://en.wikipedia.org/ wiki/Introsort
我过去为嵌入式平台编写了相当多的软件(汽车娱乐系统、电话、游戏机等),并且我总是确保对递归设置上限深度或首先避免递归。
因此,我的程序都没有因堆栈溢出而死掉,并且大多数程序都对 32kb 堆栈感到满意。 一旦您需要多个线程,因为每个线程都有自己的堆栈,这会带来很大的回报。这样您可以节省兆字节的内存。
Yes, avoiding recursion is a good thing on all embedded platforms.
Not only does it lowers or even removes the chance of a stack-overflow, it often gives you faster code as well.
You can always rewrite a recursive algorithm to be iterative. That's not always practical though (think quicksort). A way to get around this is to rewrite the algorithms in a way that the recursion depth is limited.
The introsort is a perfect example how it's done in practice. It limits the recursion depth of a quicksort to log2 (number-of-elements). So on a 32 bit machine you will never recurse deeper than 32.
http://en.wikipedia.org/wiki/Introsort
I've written quite a bit of software for embedded platforms in the past (car entertainment systems, phones, game-consoles and the like) and I always made sure that I put a upper limit on the recursion depth or avoided recursion at the first place.
As a result none of my programs ever died with a stack-overflow and most programs are happy with 32kb of stack. This pays off big time once you need multiple threads as each thread gets it's own stack.. You can save megabytes of memory that way.
我看到几个答案可以归结为“不要使用递归”。 我不同意——iPhone 并不像是某种受到严格限制的嵌入式系统。 如果问题本质上是递归的,请随意以这种方式表达它。
除非您递归到数百或数千帧的堆栈深度,否则永远不会遇到问题。
I see a couple of answers that boil down to "don't use recursion". I disagree - it's not like the iPhone is some severely-constrained embedded system. If a problem is inherently recursive, feel free to express it that way.
Unless you're recursing to a stack depth of hundreds or thousands of frames, you'll never have an issue.
iPhone 上的最大堆栈大小?
iPhone 运行修改后的 OSX,其中每个进程都被赋予有效的内存空间,就像大多数操作系统一样。
它是一个完整的处理器,因此堆栈会增长,而堆会下降(反之亦然,具体取决于您的观点)。 这意味着在分配给程序的内存用完之前,堆栈不会溢出。
出于堆栈和性能原因(相对于简单循环,函数调用成本较高),最好避免递归,但无论如何,您都应该决定对递归函数施加哪些限制,并在递归函数太长时将其截断。
The max stack size on the iphone?
The iPhone runs a modified OSX in which every process is given a valid memory space, just like in most operating systems.
It's a full processor, so stack grows up, and heap grows down (or vice versa depending on your perspective). This means that you won't overflow the stack until you run out of memory allocated to your program.
It's best to avoid recursion when you can for stack and performance reasons (function calls are expensive, relative to simple loops), but in any case you should decide what limits you can place on recursive functions, and truncate them if they go too long.