Workaround for horrendously slow SUBSTR Character Parsing in FoxPro
July 03, 2020 • N/A
It'll be faster if you copy the string into memory and then use SYS(2600) to read byte by byte.
LOCAL lnX, lcString
lcString = REPLICATE("1234567890",100000)
Declare Integer HeapAlloc in Win32Api Integer, Integer, Integer
Declare Integer HeapFree in Win32APi Integer, Integer, Integer
Declare Long GetProcessHeap in Win32API
lnLength = LEN(lcString)
TRANSFORM(lnLength,"9,999,999")
IF .T.
lnSecs = SECONDS()
lnBase = HeapAlloc( GetProcessHeap(), 0, m.lnLength )
Sys(2600, m.lnBase, m.lnLength, m.lcString)
FOR lnX = 1 TO lnLength
lcVal = Sys(2600,m.lnBase-1+m.lnX,1)
ENDFOR
HeapFree( GetProcessHeap(), 0, m.lnBase )
? "SYS(2600): " + TRANSFORM(SECONDS() - lnSecs )
ENDIF
That's 0.33 seconds on my machine vs 37 seconds with SUBSTR().
@Christof - that's great! Another great solution - looks like roughly on par with Marco's improvement.
Christof Wollenhaupt
July 05, 2020