Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to do Factorials in VFP with Recursion
Message
 
 
À
23/02/2000 00:46:05
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00335196
Message ID:
00335848
Vues:
22
Vlad,

Well I had a misplaced ) so my code was really only testing the boundary condition of doing 0! so it's a jaded benchmark for sure. *L*

Fixing that error I get similar results that the recursive is only 1/2 as fast as nonrecursive. Personally I wouldn't throw out the elegance of the recursive definition unless it was around an order of magnitude slower. The recursive is still doing 8300 factorials per second, which is likely to be fast enough.

If absolute performance was the main issue using code like:
x = seconds()
dimension gaFactorial[20]
gaFactorial[1] = 1
for i = 2 to 20
   gaFactorial[i] = i * gaFactorial[i-1]
endfor

for i = 1 to 10000
   y = gaFactorial[ ceil( rand() * 20 ) ]
endfor

? seconds() - x
Gives an order of magnitude performance improvement over the nonrecursive method.

Several threads here have discussed that easier to maintain code is by far better than complex code that runs faster. I agree with that theory. I'd rank the code complexity of these Factorial functions, from least to most complex: recursive, nonrecursive, lookup.

>You challenge me DF, and this is REALLY bad! :)
df (was a 10 time MVP)

df FoxPro website
FoxPro Wiki site online, editable knowledgebase
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform