Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Which is faster?
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00903686
Message ID:
00903895
Views:
17
Hello Dawa

I don't think your test is accurate:
- the 3 different solutions in your test differ in the way they open the table and set the index
- you are not resetting/clearing the memory, etc. after each test.

the following is a modified version of your test. i have also changed the number of records created (took too long on my computer) and the loop count for the last test(since i have now lesser records) .

as a result of changing the table opening and the resetting of the memory i now have solution 2 as the fastest solution:
Solution 1 = 2.324
Solution 2 = 2.283
Solution 3 = 18.286
please see if you get the same results now
*whoisfaster.prg
CLEAR
DELETE FILE table1.*

CREATE TABLE table1 FREE (field1 I)
INDEX ON field1 TAG field1 OF table1.CDX ASCENDING
LOCAL lcCaption
m.lcCaption = _VFP.CAPTION
FOR i = 1 TO 2^13
	_VFP.CAPTION = TRANSFORM(i)
	INSERT INTO table1 VALUES (RAND()*1000000000)
NEXT
_VFP.CAPTION = m.lcCaption
USE IN table1

LOCAL lnCount
m.lnCount = 2^10

release all except lnCount
* *** clear unused buffers
SYS(1104)
*Approach 1
LOCAL lnSeconds
m.lnSeconds = SECONDS()
FOR i = 1 TO m.lnCount
	=approach1()
NEXT
?"Approach 1"
?SECONDS() - m.lnSeconds
* Result:
*Approach 1
* 0.406

release all except lnCount
* *** clear unused buffers
SYS(1104)
*Approach 2
m.lnSeconds = SECONDS()
FOR i = 1 TO m.lnCount
	=approach2()
NEXT
?"Approach 2"
?SECONDS() - m.lnSeconds
* Result:
*Approach 2
* 0.422

release all except lnCount
* *** clear unused buffers
SYS(1104)
*Approach 3
m.lnSeconds = SECONDS()
FOR i = 1 TO m.lnCount
	*10 && m.lnCount I had to change this to much smaller because it took too long
	=Approach3()
NEXT
?"Approach 3"
?SECONDS() - m.lnSeconds
* Result:
*Approach 3
* 18.297

DELETE FILE table1.*

FUNCTION approach1
	LOCAL lnMax
	USE table1
	* ORDER field1
	set order to field1
	GO BOTTOM
	m.lnMax = table1.field1
	USE IN table1
	RETURN m.lnMax
ENDFUNC && approach1

FUNCTION approach2
	LOCAL lnMax
	USE table1
	SET ORDER TO field1 DESC
	LOCATE
	m.lnMax = table1.field1
	USE IN table1
	RETURN m.lnMax
ENDFUNC && approach2


FUNCTION approach3
	LOCAL ARRAY lnMax(1)
	USE table1
	SET ORDER TO field1 DESC
	SELECT TOP 1 field1 FROM table1 ORDER BY field1 DESC INTO ARRAY lnMax
	USE IN table1
	RETURN m.lnMax(1)
ENDFUNC && approach3
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform