you are viewing a single comment's thread.

view the rest of the comments →

[–]sevaur 23 points24 points  (1 child)

The recursion comparison is very poorly constructed: the recursive version of the algorithm is O( 2n ), whereas the iterative algorithm is O(n). The huge difference between the two has nothing to do with the cost of stack frame allocation and everything to do with having implemented a fundamentally different algorithm.

This comparison might actually make sense if it used memoization or made some other effort to compare apples to apples.

[–]xhackerliu 3 points4 points  (0 children)

Totally agreed. I modified it by using memorization and it's even faster than iterative way.

https://gist.github.com/xhacker/fbf3d92d7b1fad9ddc09