Macro Subst, Name Expr, and EVALUATE() Runtime EvaluationID: Q125046 2.60a 3.00 WINDOWS kbprg The information in this article applies to:
SUMMARYThis article describes and explains the differences in behavior between the three Runtime Evaluation tools provided by the FoxPro language: "&" (macro substitution), "()" (name expression), and the "EVALUATE()" function.
MORE INFORMATIONThe "Language Reference" guide specifies that EVALUATE() and name expressions should be used instead of macro substitution because they run faster. However, it does not explain why this is so.
Macro SubstitutionMacro substitution is just what it is called. A runtime "macro" is built into the tokenized code to evaluate the expression at runtime. The compiler inserts some compiling-linking code into the application, and when the code is run, on-the-fly compilation and linking takes place to tokenize the expression before executing it. This is highly inefficient for two reasons:
This is the preferred method for referencing FoxPro components that can be named, like windows, menus, popups, and files (including screens, menus, and reports) in the commands that run them, as in the following examples:
EVALUATE()This function is, like every FoxPro function and command, built into the language library and therefore more efficient than macro substitutions because there is no need for on-the-fly compilation. EVALUATE() can be used to return the values of expressions anywhere in the code, except when name expressions are required (see above). Typically, this function is used to retrieve sorting, filtering, or querying information from the user at runtime, as the following example illustrates:
Special ConsiderationsEVALUATE() VS. MACRO SUBSTITUTION IN LOOPING CONDITION: Consider the following syntax:
In the above example, if &cCondition is used as the condition, the loop
will process one record only, because the condition is evaluated only when
the mini-compiler executes on-the-fly. Instead, the condition should be
"EVALUATE(cCondition)" (without the quotation marks), because EVALUATE()
can be called repeatedly because it is a native FoxPro function.
CODE OPTIMIZATION: To speed up runtime execution, developers usually reduce the number of lines in their code, thinking that if the size in memory is reduced, the application will execute faster. For example, the following would be a valid optimization:
While it is true that reducing the size of the code in memory generally
helps speed up the code at runtime, it is not always true that reducing the
number of lines reduces the size of the application in memory, especially
when using macro substitution, because of the extra code that is built in
by the compiler. Also, even if the application size in memory is reduced,
the overhead incurred during macro evaluation slows the execution down
anyway. The following example illustrates this:
Note that these optimization concepts are fairly irrelevant with simplistic
examples like these. In a real-world application, the speed difference will
be more noticeable.
For more information about the EVALUATE() function, please see the following articles in the Microsoft Knowledge Base:
OVERLAPPING OF USAGE:
In some instances, EVALUATE() and name expressions can be interchanged. In this case, there is no preferred method, although it would help the developer to use name expressions whenever possible for consistency's sake. Here is a situation where either method is acceptable:
Although macro substitution could also be used here, its use is not
demonstrated because of the reasons explained previously: speed,
efficiency, and consistency.
Additional reference words: VFoxWin 3.00 2.60a FoxWin named expression KBCategory: kbprg KBSubcategory: FxprgMacrosub
|
Last Reviewed: May 1, 1996 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |