Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
A question about SEEK()
Message
 
To
17/03/2000 10:33:43
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00346748
Message ID:
00347043
Views:
24
>George,
>
>>...but if the record set is 1,000+ you can write a binary search that'll be about four times faster than locate
>
>So have you, and where is it?

I should have said "up to four times" (actually about 3.9, but I rounted it up. What I've listed below is the test code I used. The underlyning view contained 1,273 records and the variable listed as "style" represented the last record in the view. The ? lcrecno was inserted to confirm that the routine was finding the correct record number. Naturally, when searching for records that are not the last one, LOCATE will perform better, but then the binary search will as well. You might note the variable llsoft. This was a leftover from actual production code used to search an ordered list. Naturally, I've modified it for this purpose. The reason that I bring this up is that: 1. The assignment statement of this variable actually slow the thing down; and 2. You could add a parameter to do a "soft search".
lcstyle = "M7378"
lnstart = SECONDS()
FOR lni = 1 TO 1000
  LOCATE FOR STYLE = lcstyle
NEXT
lnstop = SECONDS()
? lnstop - lnstart

lnstart = SECONDS()
lcsearch = 'foo.style'
FOR lni = 1 TO 1000
  lnrecno = BSearch(lcstyle, lcsearch)
NEXT
lnstop = SECONDS()
? lnstop - lnstart
? lnrecno

FUNCTION BSearch
  
  LPARAMETER pSearchFor, pcfield
  
  LOCAL lnresult, lnfirst, lnlast, lnmid,;
  llfound, llsoft
  llfound = .F.
  lnresult = 0
  lnfirst = 1
  lnlast = RECCOUNT()
  llsoft = .F.
  DO WHILE lnfirst  foo.style
        lnfirst = lnmid + 1
      ELSE
        lnlast = lnmid - 1
      ENDIF
    ENDIF
  ENDDO
  IF llfound
    lnresult = lnmid
  ELSE
    IF llsoft
      lnresult = lnfirst
    ENDIF
  ENDIF
  RETURN lnresult
ENDFUNC
George

Ubi caritas et amor, deus ibi est
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform