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:
00903879
Views:
14
Hi,Guys,

I have written the follwing prg to run the test, and I've included the test result in the code comment. My test shows that "go bottom" is fastest among the three approaches.
*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^20
	_vfp.Caption = TRANSFORM(i) 
	INSERT INTO table1 VALUES (RAND()*1000000000) 
NEXT 
_vfp.Caption = m.lcCaption 
USE IN table1 

LOCAL lnCount
m.lnCount = 2^10
*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

*Approach 2
m.lnSeconds = SECONDS() 
FOR i = 1 TO m.lnCount
	=approach2() 
NEXT 
?"Approach 2"
?SECONDS() - m.lnSeconds 
* Result:
*Approach 2
* 0.422


*Approach 3
m.lnSeconds = SECONDS() 
FOR i = 1 TO 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 
	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
Dawa Tsering


"Do not let any unwholesome talk come out of your mouths,
but only what is helpful for building others up according to their needs,
that it may benefit those who listen."

- Ephesians 4:29-30 NIV

Dare to Question -- Care to Answer

Time is like water in a sponge, as long as you are willing you can always squeeze some.

--Lu Xun, Father of Modern Chinese Literature

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform