Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Reverse comma-delimited string
Message
From
12/09/2019 15:46:28
 
 
To
12/09/2019 13:25:07
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01670542
Message ID:
01670812
Views:
46
>That sounds interesting. I am dealing a lot with large strings and files, and that could significantly improve performance.

I'll work on it tonight. What are some of the API functions you'd like to see? I'll use a FSTR prefix, for "fast string."
* Allocate a new string for fast concatenation:
lcName = FSTRNEW(nSize) -- Allocate a new string of default nSize (can be 0)
* Note:  lcName is a placeholder.  It's not the real data.  Use FSTRDATA() to get the real data

* Fast insert string before:
FSTRBEF("new text to prepend", lcName)

* Fast insert string after:
FSTRAFT(lcName, "new text to append")

* Fast arbitrary insert:
FSTRMID(lcName, nStartingChar, "new text to insert")

* Get the length
FSTRLEN(lcName)

* Reset for continued use
FSTRZAP(lcName)

* Release memory associated with the lcName variable
FSTRFREE(lcName)

* Delete a portion
FSTRDEL(lcName, nStart, nLength)

* Perform a CHRTRAN()
FSTRCHRTRAN(lcName, cSearchString, cReplaceString)

* Extract a portion
lcString = FSTRSUBSTR(lcName, nStart, nLength)

* Extract it all
lcString = FSTRDATA(lcName[, .t.])   && .t. -- Delete after extract?
Example use:
lcName = FRSTNEW()

FOR lnPass = 1 to 5
    RAND(0)
    lnSeconds2 = SECONDS()
    FOR lnI = 1 to 300000
        DO CASE
            CASE lnI % 3 = 0
                * Prepend
                FSTRBEF(TRANSFORM(lnI), lcName)

            CASE lnI % 3 = 1
                * Append
                FSTRAFT(lcName, TRANSFORM(lnI))

            OTHERWISE
                * Stuff at a random place in the middle
                FSTRMID(lcName, 1 + INT(RAND() * (FSTRLEN(lcName) - 1)), TRANSFORM(lnI))
        ENDCASE
    NEXT
    ? "Pass " + TRANSFORM(lnPass), SECONDS() - lnSeconds2

    * Save the data
*    STRTOFILE(FSTRDATA(lcName), SYS(2015) + "_" + TRANSFORM(lnPass) + ".txt")

    * Reset for the next pass
    FSTRZAP(lcName)
NEXT

* Release when finished
FSTRFREE(lcName)
RELEASE lcName
Compared to a native VFP equivalent example:
lnSeconds = SECONDS()
lcName = SPACE(0)
FOR lnPass = 1 to 5
    RAND(0)
    lnSeconds2 = SECONDS()
    FOR lnI = 1 to 300000
        DO CASE
            CASE lnI % 3 = 0
                * Prepend
                lcName = TRANSFORM(lnI) + lcName

            CASE lnI % 3 = 1
                * Append
                lcName = lcName + TRANSFORM(lnI)

            OTHERWISE
                * Stuff at a random place in the middle
                lcName = STUFF(lcName, 1 + INT(RAND() * (LEN(lcName) - 1)), 0, TRANSFORM(lnI))
        ENDCASE
    NEXT
    ? "Pass " + TRANSFORM(lnPass), SECONDS() - lnSeconds2

    * Save the data
*    STRTOFILE(lcName, SYS(2015) + "_" + TRANSFORM(lnPass) + ".txt")

    * Reset for the next pass
    lcName = SPACE(0)
NEXT

* Release when finished
RELEASE lcName
? "Native VFP", SECONDS() - lnSeconds
On my computer, the above VFP code took:
100,000 -- 20.4 seconds
200,000 -- 63 seconds
300,000 -- 170 seconds
For a control, I took off the concatenation operations and just ran the TRANSFORM(lnI) operations. Those took:
100,000 -- 0.096 seconds
200,000 -- 0.199 seconds
300,000 -- 0.289 seconds
It shows the bulk of the operation in processing is in the string manipulation. I'm curious what the fst*() functions could do.

Please provide alternate idea / suggestions. I'll implement what's needed for people.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform