I've never taken much advantage of UDF's. I recently started putting much more often used logic into functions. Curious as to the difference in using these functions in large batch scripts or Alpha operations with many records as opposed to keeping the expression at the execution point, I ran a little test. I used a function posted by Ira for finding the last day of a month. Here it is.
''XBasic
dim datevalue as d
datevalue = {01/01/01}
'rem function test
t1 = toseconds(time())
For x = 1 to 50000
lastday = monthend(datevalue)
Next x
t2 = toseconds(time())
trace.writeln("Function Test for " + dtoc(lastday) + " - Seconds elapsed:"+STR(t2-t1))
'rem non function test
t1 = toseconds(time())
For x = 1 to 50000
lastday = if(DateValue={},{},Addmonths(DateValue-Day(DateValue)+1,1)-1)
Next x
t2 = toseconds(time())
trace.writeln("Non Function Test for " + dtoc(lastday) + " - Seconds elapsed:"+STR(t2-t1))
end
'rem code for monthend function
'function MonthEnd as D(DateVal as D)
' MonthEnd=iif(DateVal={},{},Addmonths(DateVal-Day(DateVal)+1,1)-1)
'end function
From the trace window:
Function Test for 01/01/2001 - Seconds elapsed: 41
Non Function Test for 01/01/2001 - Seconds elapsed: 10
The only difference I can see is that caused by calling the function. I realize that the time difference between the two processes divided by 50000 is small (6 ten thousandths of a second), but my conclusion is that I will still use functions much more frequently, just not in large batch processes or queries where the function has to be called in every iteration.
John
''XBasic
dim datevalue as d
datevalue = {01/01/01}
'rem function test
t1 = toseconds(time())
For x = 1 to 50000
lastday = monthend(datevalue)
Next x
t2 = toseconds(time())
trace.writeln("Function Test for " + dtoc(lastday) + " - Seconds elapsed:"+STR(t2-t1))
'rem non function test
t1 = toseconds(time())
For x = 1 to 50000
lastday = if(DateValue={},{},Addmonths(DateValue-Day(DateValue)+1,1)-1)
Next x
t2 = toseconds(time())
trace.writeln("Non Function Test for " + dtoc(lastday) + " - Seconds elapsed:"+STR(t2-t1))
end
'rem code for monthend function
'function MonthEnd as D(DateVal as D)
' MonthEnd=iif(DateVal={},{},Addmonths(DateVal-Day(DateVal)+1,1)-1)
'end function
From the trace window:
Function Test for 01/01/2001 - Seconds elapsed: 41
Non Function Test for 01/01/2001 - Seconds elapsed: 10
The only difference I can see is that caused by calling the function. I realize that the time difference between the two processes divided by 50000 is small (6 ten thousandths of a second), but my conclusion is that I will still use functions much more frequently, just not in large batch processes or queries where the function has to be called in every iteration.
John
Comment