General information
Category:
Internet applications
Title:
Benchmarking VFP for string processing.
Hello everyone,
While programming for the internet, I had to do lots of text processing, so I decided to benchmark the various methods of using variables. Thought I'd share my conclusions with you.
Conclusion:
If you are accessing string variables in a loop, avoid arrays and class variables.
1. Class variables are 50 times slower than local vars.
2. Arrays are sometimes 100 times slower than local vars. This was a surprise.
3. The overhead of using a function call with RETURN is minimal.
4. Call by reference (@var) is the slowest. 160 times slower than local vars. Probably best to avoid. This was a B-I-G surprise for me.
5. Global variables are just as fast as local vars.
RESULTS
Test method: Append long string to variable 1000 times.
Example:
timert = seconds()
str = ''
for i = 1 to 1000
str = str + '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
endfor
outp('BASELINE=',seconds() - timert)
Results:
Append to local var 1000 times: 0.03 secs
Append to class var 1000 times: 1.35 secs
Append to 6th element of array 1000 times: 3.12 secs
Call a function by reference to append string: 3.20 secs
for i = 1 to MAXNUM
bmCallByRef(@str) && this appends to str by ref
endfor
Call a function that returns a string: 0.13 secs
for i = 1 to MAXNUM
str = str + bmReturn() && returns a string
endfor
Use a global variable in a function: 0.07 secs
================ SOURCE CODE BELOW
* You might have to replace outp with you own messagebox function
* Find out which parameter passing method is fastest
FUNCTION HTMLBenchmark1
local timert,i,str
public gBMStr
*!* BASELINE=0.03
*!* bmClassStr=1.35
*!* bmArray=3.12
*!* BMCALLBYREF=3.20
*!* BMRETURN=0.13
*!* BMGLOBAL=0.07
*!* for 1000 iterations
#define MAXNUM 1000
outp("Starting ",MAXNUM," iterations")
timert = seconds()
str = ''
for i = 1 to MAXNUM
str = str + ;
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
endfor
outp('BASELINE=',seconds() - timert)
o = createobject('wwBM')
timert = seconds()
for i = 1 to MAXNUM
o.mStr = o.mStr + ;
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
endfor
outp('bmClassStr=',seconds() - timert)
timert = seconds()
local arr[2,4]
arr[2,3] = ''
for i = 1 to MAXNUM
arr[2,3] = arr[2,3] + ;
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
endfor
outp('bmArray=',seconds() - timert)
timert = seconds()
str = ''
for i = 1 to MAXNUM
bmCallByRef(@str)
endfor
outp('BMCALLBYREF=',seconds() - timert)
timert = seconds()
str = ''
for i = 1 to MAXNUM
str = str + bmReturn()
endfor
outp('BMRETURN=',seconds() - timert)
timert = seconds()
gBMStr = ''
for i = 1 to MAXNUM
BMGLOBAL()
endfor
outp('BMGLOBAL=',seconds() - timert)
ENDFUNC
DEFINE CLASS wwBM as CUSTOM
mOther = .f.
mFun = .f.
mStr = ''
mHaha = .f.
ENDDEF
FUNCTION BMGLOBAL()
gBMStr = gBMStr + ;
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
FUNCTION bmCallByRef(z)
z = z + ;
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
FUNCTION bmReturn()
local z
z = ;
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890'
return z
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only