Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Constants and SYS(5) + SYS(2003)
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00267724
Message ID:
00268676
Vues:
42
Hi Roi,
>
>I'm jumping back into the fray < g >. My this is confusing, isn't it. I did a little more reading last night on these constants and gained a little more understanding in how VFP uses them.

Terrific. Always good to have your input.

>What happens at compile time is VFP just does a string replacement of the constants. No evaluation happens. If you #define SOME_CONSTANT 'MyExpression' the compiler just does a global search and replace of SOME_CONSTANT, then compiles the code. At run-time as VFP is executing the code SOME_CONSTANT doesn't exist, it has been replaced with 'MyExpression'. You can see this with following. Set logerrors on and compile the following:
>#define TEST_CONSTANT 232'
>x = TEST_CONSTANT + 1

You will get the following error:
>x = 232' + 1
>Error in line 2: Syntax error.

The constant has been replaced with it's value. If the constant was evaled at run-time the compiler would ignore the syntax error during compiling and the error would surface during program execution would it not?
>
>Also in the hacker's guide is an example of how to defeat that behaviuor by bracketing the constant. Check it out, run the sample and pay attention to the c_tVERSION_BUILT constant. Its wrong, you think it will store the time when it was compiled, but it doesn't it returns the current time.
>
>In light of that I don't think that constants will give you any performance gain simply because the evaluation happens at run-time, not at compiile time.
>
>So what do you think? Am I understanding this correctly or am I still missing something.

Actually, you do (and are supposed to) gain in performance because of the fact that compile time constants don't have to be re-evaluated at run-time. Try the following test. You can do it by executing the code in a prg by highlighting it. Before you do so, look at the code and see if you can figure out which is fastest and why (No cheating by looking at what I say afterwards< g >).
#DEFINE SHORT_STRING "AAAAA"

lcshort = "AAAAA"
lnstart = SECONDS()
FOR lni = 1 TO 100000
  lcfoo = SHORT_STRING
NEXT
lnstop = SECONDS()
? lnstop - lnstart
lnstart = SECONDS()
FOR lni = 1 TO 100000
  lcfoo = lcshort
NEXT
lnstop = SECONDS()
? lnstop - lnstart
lnstart = SECONDS()
FOR lni = 1 TO 100000
  lcfoo = "AAAAA"
NEXT
lnstop = SECONDS()
? lnstop - lnstart
You didn't peek down here, did you?< bg > OK, here's what I'll bet you saw: Each time you ran it, the first loop was fastest, the next fastest was the last, and the slowest was the second loop. However, if you replace the defined constant with REPLICATE("A", 5), the first instance will drop to the slowest because it's being re-evaluated on each pass.
George

Ubi caritas et amor, deus ibi est
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform