Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Recursion very slow
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01086980
Message ID:
01087000
Vues:
22
I can't really find anything wrong with your code just by looking at it. I don't have time to run it tonight but I might try to run it tomorrow. I find it odd that C# starts slowing down with such relatively low numbers (x = 25, n = 20). C# is probably not the best language to run recursion if you want it to run really really fast, because C# compiles to IL and not to machine code.
If you want it to run really really (really) fast you will probably have to do this in C (there might also be some comilers that can optimize this better than others). (I recall rewriting some really complex looped [looping through over a million datapoints] calculations in C when I was in college and it improved the speed by a factor over 1000 when compared to IDL and MatLab)

Einar

>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;
>
>        }
>
Semper ubi sub ubi.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform