Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Argument starter - The roots of all evil
Message
De
08/09/2004 11:07:44
Walter Meester
HoogkarspelPays-Bas
 
 
À
08/09/2004 08:56:14
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00938079
Message ID:
00940289
Vues:
41
Tamar,

>Tested VFP 8 SP1 as well. The difference isn't quite as large there, but I'm still seeing 15.4830 seconds for DO WHILE and 1.0530 seconds for FOR.

>Seems to me there's a fairly strong case for use a FOR loop when what you're doing is basically a counted operation even if you'll need a LOOP or EXIT.


I'm really disappointed in such statistical conclusion. It is just these kind of conclusions that make me pull out my hair of frustration.

First of all you probably did your test with an empty loop like.
nSec = SECONDS()
nT = 1
DO WHILE nT < 1000000
	nT = nT + 1 
ENDDO

WAIT WINDOW SECONDS() - nSec


nSec = SECONDS()
FOR nT = 1 TO 1000000
ENDFOR
WAIT WINDOW SECONDS() - nSec
In this case there is no doubt your observation makes sence. I've got a result of 1.031 for DO WHILE and 0,088 for FOR NEXT: A 1171% performance difference. Seems quite convincing.

However, I don't know any applications for an empty loop, So lets change the example a little:
nSec = SECONDS()

nSum = 0
nT = 1
DO WHILE nSum < 10000000000
	nT = nT + 1 
	nSum = nSum + nT
ENDDO
? nT
WAIT WINDOW SECONDS() - nSec


nSec = SECONDS()
nSum = 0

FOR nT = 1 TO 1000000
	nSum = nSum + nT
	IF nSum > 10000000000
		EXIT
	ENDIF
ENDFOR
? nT
WAIT WINDOW SECONDS() - nSec
This is a very simple and fast example for summing up all integers from 1 to [ n ] until they reach a certain value. If you run this example you'll notice that the difference between the two implementation is narrowed down to about 38% (in 141421 iterations), and this is body is very very simple. If it becomes more complex than this the difference very quickly becomes irrelevant.

The difference between DO WHILE and FOR NEXT is that the for next is very efficiently implemented in VFP and the DO WHILE always requires evaluation of the argument and an extra line to increment the counter. This is an overhead that will prove about insignificant when using an IF condition and EXIT in a FOR NEXT loop.



Walter,
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform