Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Recursion very slow
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01086980
Message ID:
01086986
Views:
23
>Hi All,
>
>I have some two equations that I want to solve with recursion:
>
>aa(x,n) = 0.5 + 1/1.03*Px[state,state]*aa(x+1,n-1)
> + 1/1.03*Px[state,state]*ia(x+1,n-1,1)
>
>ia(x,n,inv) = 0.5 + 1/1.03*Px[state,state]*aa(x+1,n-1)
> + 1/1.03*Px[state,state]*ia(x+1,n-1,inv+1)
>
>where aa(x,0) = 0 and ia(x,0,inv) = 0 and ia(x,n,inv) = 0 for inv > 6.
>
>I have set up the following c# code for this,it works to some extent. But it is very slow. For x = 25, n = 20, it takes an eternity, while x =10, n =10 is pretty fast.
>
>If someone would help me with this, it would be very very appreciated.
>
>Thanks beforehand.
>
>
>
>
>public double aa(int x, int duur)
>        {
>            if (duur == 0) return 0;
>            this.SetPx(x, TransitionType.met_terugkeer);
>            double[,] thePx = this.Px;
>
>            double aax1n1 = 1 / 1.03 * thePx[(int)Toestanden.actief, (int)Toestanden.actief] * aa(x + 1, duur - 1);
>            double iax1n1 = 1 / 1.03 * thePx[(int)Toestanden.actief, (int)Toestanden.invalide1] * ia(x + 1, duur - 1, 1);
>            // this.PremieStroom[duur]
>           return 0.5  + aax1n1 + iax1n1;
>      }
>
>        // voorwaardelijke activiteitsrente na invalide worden
>  public double ia(int x, int duur,int invaliditeit)
>        {
>            //this.SetPx(x, TransitionType.met_terugkeer);
>            double[,] thePx = this.Px;
>            if(duur == 0||invaliditeit > 6) return 0;
>
>            double aax1n1 = 1 / 1.03 * thePx[invaliditeit, (int)Toestanden.actief] * aa(x + 1, duur - 1);
>            double isax1n1 = 1 / 1.03 * thePx[invaliditeit, invaliditeit + 1] * ia(x + 1, duur - 1, invaliditeit + 1);
>            return  0.5 + aax1n1 + isax1n1;
>
>        }
>
Zakaria,

I am not a mathematician, but the problem with slow recusrions are generally caused by two computing bottle necks:
1. Stack management: The way the machine manages stacks of recursion varies from OS/Language to OS/language.
2. Memory Management: (somewhat linked to the first reason) When the machine starts to swap memory to a physical medium (disk), it will exponentially slow down the process, since at that point in time the whole stack space is being swapped to/from diak.

How do we deal with this issues:
1. Turn off virtual memory on the machine, thus avoiding swapping to physical media. The trade off is that you will most likely get "Out of Memory" issues, but not always depending on the memory management of the OS you are using (XP and 2003 tend to be very forgiving).
2. Trace the iterations (Iteration N/ Start time/End Time/Time Delta). You can set a public counter, set to -1 and increment by one everytime the function is called (the initial call with be zero, which is your entry point) and log this to the debug output window. Remember that the time values you want to evaluate are from Start Time n-1 to start Time n, and that end time will be a cummulative from first iteration to last (Start Time 0 to End Time n of iteration).

Based on the numbers on #2 you will be able to trace at what point the bottle neck is taking place. Also, in VS 2003/2005 you can turn on code optimization.

I hope this helps you somewhat.
Ricardo A. Parodi
eSolar, Inc.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform