我应该避免 iPhone 上的递归吗?

发布于 2024-07-15 19:10:56 字数 73 浏览 6 评论 0原文

我应该避免在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天气好吗我好吗 2024-07-22 19:10:56

是的,避免递归在所有嵌入式平台上都是一件好事。

它不仅可以降低甚至消除堆栈溢出的可能性,而且通常还可以为您提供更快的代码。

您始终可以将递归算法重写为迭代算法。 但这并不总是实用(想想快速排序)。 解决这个问题的一种方法是以限制递归深度的方式重写算法。

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.

凉宸 2024-07-22 19:10:56

我看到几个答案可以归结为“不要使用递归”。 我不同意——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.

酒浓于脸红 2024-07-22 19:10:56

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文