Recursion or Iteration?

前端 未结 30 2417
小鲜肉
小鲜肉 2020-11-22 14:44

Is there a performance hit if we use a loop instead of recursion or vice versa in algorithms where both can serve the same purpose? Eg: Check if the given string is a palind

30条回答
  •  情深已故
    2020-11-22 15:30

    Mike is correct. Tail recursion is not optimized out by the Java compiler or the JVM. You will always get a stack overflow with something like this:

    int count(int i) {
      return i >= 100000000 ? i : count(i+1);
    }
    

提交回复
热议问题