Hi David,
My initial purpose was not to test something or compare something. I just have a class with a function that reads an input table and based on this table and some inputparameters it creates a new output table. What I wanted was to check if the input table is already "read" and the output table is already created, if so I don't have to do it again.
I noticed that it took a while for checking this, my first thought it could not take so long and this was caused by the fact that I was using a class function. So thas was the reason for comparing the pieces of code.
But indeed, as you said it is not an unbaised comparison. The delay is caused by calling a function in general, in fact the class function runs in this case almost as fast as a standalone function.
Still then I find calling a function in a loop "very" slow. I know that I need to use this check a lot for a specific application.
Thanks all for your input.
>Zakaria,
>
>Well the big thing is it's not the
exact same code.
>
>In the case of the method, you are pushing an argument onto the call stack, looking up the method entrypoint, and calling it. Then 1 time you access the argument, and the other 999,999 times you don't.
>
>You'd see a somewhat similar slowdown if you used a regular function call relative to the straight code loop. But calling functions is faster by a small amount than calling methods.
>
>Honestly this isn't a code fragment that can be really optimized anyway. Any code that would attempt to check to see if a table was used inside a tight loop should be hoisted out of the loop.
>
>>How does it come that if I run an exact piece of code alone is ~ 7 times faster than that I run the code in a function from a class object. I don't understand why it takes so long, I would appreciate if someone could help me optimize the following:
>>
>>**** speedtest
>>LOCAL loTest as ActSymbolen
>>cFile = "C:\Actuariaat\Input\gbmgbv.dbf"
>>loTest = CREATEOBJECT("ActSymbols" )
>>
>>time1 = SECONDS()
>>
>>
>>*** test 1 : using class function
>>
>>FOR klm = 1 TO 1000000
>>
>>loTest.ReadLxTables(cFile)
>>ENDFOR
>>? "using class function ",SECONDS() - time1
>>
>>SET dataSESSION TO 2
>>CLOSE DATABASES ALL
>>
>>
>>time2 = SECONDS()
>>
>>*** test 2: without using class function
>>FOR klm = 1 TO 1000000
>>IF !USED("Gbmgbv") THEN
>> USE (cFile) IN 0
>> SELECT "gbmgbv"
>>
>>ELSE
>> SELECT "gbmgbv"
>>ENDIF
>>ENDFOR
>>? "without using class function" ,SECONDS() - time2
>>
>>
>>
>>
>>DEFINE CLASS actSymbols as Session
>>
>>
>>FUNCTION ReadLxTables
>>LPARAMETERS cFile
>>
>>IF !USED("Gbmgbv") THEN
>> USE (cFile) IN 0
>> SELECT "gbmgbv"
>>
>>ELSE
>> SELECT "gbmgbv"
>>ENDIF
>>
>>ENDFUNC
>>
>>
>>
>>ENDDEFINE
>>