From the previous article, we learned that to prevent exceptions in overflow, we can use continuation. The previous article also tells us how to convert normal Recursion TO continuation, we will consider one question: Why don't we directly use continuation? Or, when the data volume is small, what should we choose?
After reading the previous article, I also thought about using the simplest Fibonacci as an example to perform a conversion from normal recursion to CSP. Let's take a look at the implementation of the ordinary recursive Fibonacci:
View code
1 module FibRecursive = 2 let rec fib n = 3 match n with 4 | 1 5 | 2 -> 1 6 | _ -> ( fib (n-1) ) + (fib (n-2) ) 7 8 fib 30
Using this article, we get the following CSP implementation results. The Code has been put into fssnip recently:
View code
1 module FibContinuation = 2 let rec fib n count = 3 match n with 4 | 1 5 | 2 -> count (1) 6 | _ -> 7 let first x = 8 let second y = 9 count(x + y) 10 fib (n-2) second 11 fib (n-1) first
Because the data for this test is not big, opening the task manager does not find much difference between the two during running. Therefore, I chose stopwatch to check their running time, put the code in stopwatch. After running the code, I get the following results:
First, the result of normal recursion is as follows:
The following is the CSP result:
Of course, you can also try to use some data to prove such a conclusion. In view of the length issue, this article only uses this data set for explanation. Therefore, from the above two figures, when the amount of data is smaller than the acceptable range of normal recursion, the efficiency of normal recursion is higher than that of CSP.
Additional articles:
When you use tail recursion in F # For the first time, you may find a strange thing. Obviously, the CSP method will not be abnormal, however, when you select F5 or Ctrl + F5 in vs editor to run the code, you will still see stackoverflowexception. Even if your data is really small and there is no exception in normal recursion, CSP throws an exception, as shown in:
This made us feel cool. Why? But when you try to use fsi fsc.exe, you will find that the code can run normally. Isn't this a bug of? When I asked Liu Tao, who has been engaged in F # language for many years at Microsoft, he told me that this is not the case. However, when vs is set to debug mode, the tail recursion function is disabled by default, so when you want to use tail recursion, you can set vs to the release mode, or you can enable the tail recursion function in debug mode:
1. Right-click the selected project in the project manager and select "properties"
2. Open the "build" tab.
3. Make sure that the "debug" configuration is selected.
4. Select the "generate tail CILS" check box, as shown in:
Well, next you can try to run your CSP program. I believe you will see the results that make you satisfied ~ Wish you a pleasant journey in F!
I wrote this article in Blogspot a few days ago. One reason is that the domestic network is not easy to access, and the other reason is that the article is in English. Therefore, the Chinese version is written here, I hope to help you. At the same time, interested friends can click the following link to visit the original: http://jeallynduan.blogspot.com/2012/11/why-continuation-function-also-throw.html http://jeallynduan.blogspot.com/2012/11/different-efficiency-between-tail.html