October 22, 2007
@ 10:23 PM

I've had these notes about optimizing VFP code hanging around a long time (and added a few a little more recently). According to the date on it, most of this is from 8/1/2002. Wow!

Most of this came out of a data conversion project that would take 3-4 hours each time it was run (and during development we needed to run this a LOT so 3-4 hours would just kill productivity).

  • () evals are much faster than macro substitution:

    REPLACE (lcField) WITH 1

    instead of

    REPLACE &lcField WITH 1

    2.5 - 3X faster

    Directly accessing a field:

    REPLACE field WITH 1

    is 8-10X faster than the REPLACE (lcField) WITH 1 code.

  • Do multiple field updates in a single REPLACE.

    For example:

    REPLACE field WITH 1, field2 WITH 2, field3 WITH 3 IN TableName

    Instead of:

    REPLACE field1 WITH 1 IN TableName
    REPLACE field2 WITH 2 IN TableName
    REPLACE field3 WITH 3 IN TableName


  • If you are doing COM interop, use WITH/ENDWITH instead of directly accessing object references - this keeps VFP from having to traverse the object hierarchy on each line. This can be significantly faster.

  • SCATTER MEMVAR is up to 10X faster than SCATTER NAME, although SCATTER NAME provides better encapsulation. If you are doing this for a large number of records, SCATTER MEMVAR may be a much better choice.

  • If you are doing a SELECT ... WHERE IN (SELECT ...) style query, check to see if it's functionally equivalent to an INNER JOIN. This can make a HUGE difference.

  • CREATEOBJECT() is much faster than NEWOBJECT().

  • Opening and closing tables is one of the slowest thing you can do. In the data conversion app. we had some code that opened a lookup table at the beginning of the function, then closed it at the end. The data conversion process was spending approx. 45 minutes of time in this routine alone. By opening the tables only once (and leaving them open), the processing time dropped to under 3 minutes (It was so much faster that I had to test this a few times to make sure I hadn't broken it).

 

OK, nothing really earth shattering here. But hopefully you'll find one or two of the above helpful at some point.


 
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, i, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview