Thursday, November 1, 2012

Different efficiency between Recursion and Continuation

As we all know, we often use recursive function in our program, and we often encounter the stack overflow exception. In blog F# Continuation Style Programming , we learn how to change the recursive into continuation to avoid this exception. Of course, if the data is small, we will think which one is better to use.

After testing the running time between Recursive and Continuation function with small data, here we use the example of Fibonacci which is known to us, then we can get the results as follows:

As the result is small to computer, there is no obvious difference performance in CPU usage and performance between Recursive and Continuation, so we measure the running time to find the result.

Below is the running time for Recursive function:
Below is the running time for Continuation function:

 
Now, we can see, if the data is small, the Recursive will cost less time than Continuation. So when the data is small, recursive function is the better choice we should choose.

No comments:

Post a Comment