Partager via


Optimizing Programs

By writing your code carefully, you can write the fastest possible programs. There are several ways to improve program performance in Visual FoxPro:

  • Following the general programming performance hints provided below.

  • Using name expressions instead of macro substitution.

  • Referencing object properties efficiently.

General Programming Performance Hints

To write the fastest programs possible, follow the recommendations listed below.

  • Choose the correct data type for your data. In particular, use the Integer data type for numeric information whenever possible, as it is processed most efficiently. Wherever possible, use Integer data types for primary and foreign key values, which will result in smaller data files, smaller (and therefore faster) indexes, and faster joins.

    Note

    For an example showing how to create a smaller (and therefore faster) index, run Solution.app, located in the Visual FoxPro ...\Samples\Solution directory. Choose View Samples by Filtered List, select Indexes from the drop-down list, and then choose Create Small Indexes Using BINTOC( ) from the list that appears.

  • Avoid reopening files, which slows performance. Instead, assign files to work areas as you open them, then use the SELECT command to choose a specific work area as needed.

  • Use FOR ... ENDFOR loops rather than DO WHILE ... ENDDO loops when possible, because they're faster.

  • When you copy data from multiple fields, SCATTER TO ARRAY is faster than SCATTER MEMVAR.

  • To use memory most efficiently, avoid creating objects before you need them, and clear objects when you finish with them to free memory.

    Tip

    You can test how much memory each object consumes by calling the SYS(1016) function.

  • Send output to the topmost window whenever possible; updating windows behind the top window is substantially slower. Causing output to scroll behind a window is nearly a worst-case event.

  • Disable status display with the SET TALK OFF command, which eliminates the overhead of screen update.

  • Set the SET DOHISTORY command to OFF to avoid updating the command window each time a program runs.

Using Name Expressions Instead of Macro Substitution

If you use name expressions instead of macro substitution, program performance will greatly improve. For example, if you assign a value to the variable cFile, a name expression created with cFile is faster than macro substitution.

cFile = "CUST"
use &cFile      && Macro substitution, slow
use (cFile)      && Name expression: faster, preferred

Referencing Object Properties Efficiently

By understanding how Visual FoxPro works with properties and objects, you can make your applications run more efficiently.

Optimizing Repeated References to a Property

When you reference an object property with the object.property syntax, Visual FoxPro must search for the object before it can access the property. If you must access the property repeatedly, this search strategy can slow performance.

To avoid referencing the same procedure repeatedly (such as in a loop), read the property value into a variable, make changes, and then set the property when you're through. For example, the following code fills a property array by first creating an array in memory, filling it, and then setting the property only once at the end:

* Copy string to a local variable
lcChar = THISFORM.cCharString
LOCAL laCharArray[256]   && Create local array
FOR nCounter = 1 to 256
   laCharArray[x] = SUBSTR(laChar,x,1)
ENDFOR
* Copy the local array to the property array
ACOPY(laCharArray,THISFORM.aCharArray)

Referencing Multiple Properties Efficiently

If you update more than one property for the object, Visual FoxPro must search for the object multiple times, which can affect performance. In the following example, the code causes Visual FoxPro to search through four objects (such as THISFORM, pgfCstInfo, pgCstName, and txtName) to find the property to be set. Because the code sets two properties, the fourfold search is done twice:

THISFORM.pgfCstInfo.pgCstName.txtName.Value = ;
 "Fred Smith"
THISFORM.pgfCstInfo.pgCstName.txtName.BackColor = ;
 RGB (0,0,0)  & Dark red

To avoid this overhead, use the WITH ... ENDWITH command. This method causes Visual FoxPro to find the object once. For example, the following example accomplishes the same task as the previous one, but faster:

WITH THISFORM.pgfCstInfo.pgCstName.txtName
   .Value = "Fred Smith"
   .BackColor = RGB (0,0,0)  & Dark red
ENDWITH

You can also store an object reference in a variable, then include the variable in place of the object reference:

oControl = THISFORM.pgfCstInfo.pgCstName.txtName
oControl.Value = "Fred Smith"
oControl.BackColor = RGB (0,0,0)  & Dark red

See Also

Reference

SELECT Command

FOR ... ENDFOR Command

SCATTER Command

Other Resources

Optimizing Applications

Optimizing Your System