I am developing a pathfinding algorithm for an Android game, and I can write a recursive version (which is nice, but needs a big stack, so I might need to create a dedicated Thread for this with bigger stacksize), and a “loop” version which uses a buffer (instead of recursion). It is also a big problem that I don’t know the size of the buffer in advance, so currently only the stack-based solution looks feasible.
I know this may be an algorithm theory or general computer science question, but perhaps it’s Android specific because the stack size is a system-specific feature after all.
Generally, which should be more efficient (speed) on Android? The stack one, or the one which relies on buffer (heap)? Note that the question approaches the problem architecture-wise (assuming that the algorithmic complexity doesn’t depend whether the algorithm is recursive or loop-based).
I know you asked for a Android specific answer, but I don’t think it’s really relevant to your problem. Two remarks
recursive algorithm, you can emulate the stack on the heap with a
stack based data structure. Some times you don’t even need this, though. This takes a bit more work, but don’t base your algorithm on artifical architectural constraints.
I can’t comment on whether the most optimal recursive solution to a problem is going to be better than the most optimal iterative solution on Android. Usually, all other things being equal, the iterative solution is going to be faster, but when you get to more complex algorithms than say, Fibonacci numbers, implementing an iterative algorithm recursively or vice-versa might make a difference.