Dynamic programming comparing recursive----to Fibonacci sequence for example

Source: Internet
Author: User

Here's how it's recursive:

Private Static int Fib_re (int  N)        {            Re_count++            ; if (n<=1)            {                return1;            }             Else             {                return fib_re (n1) + fib_re (n-2);}        }

Here is the dynamic plan:

Private Static int Fib_fast (int n,dictionary<intint> dic)        {            Fa_count++            ; if (! DiC. ContainsKey (n))            {                dic. ADD (n, fib_fast (n-1, DIC) +fib_fast (n-2, dic);            }             return dic[n];        }

Test process:

            Const intNUM = +; Stopwatch SW=NewStopwatch (); Sw.            Start (); intResult_re =fib_re (NUM); Sw.            Stop (); Console.WriteLine ("fib_re ({0}) ={1}: Time: {2}, Times: {3}", Num,result_re, SW.            Elapsedticks, Re_count); Sw.            Reset (); Sw.            Start (); Dictionary<int,int> dic =Newdictionary<int,int>(); Dic. ADD (0,1); Dic. ADD (1,1); intRESULT_FA =Fib_fast (num,dic); Sw.            Stop (); Console.WriteLine ("Fib_fast ({0}) ={1}: Time: {2}, Times: {3}", NUM,RESULT_FA, SW. Elapsedticks, Fa_count);


Time is nearly 20,000 times times ...

Dynamic programming comparing recursive----to Fibonacci sequence for example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.