Post by tsh73 on Dec 8, 2023 7:28:47 GMT -5
Trying to speed up rotating donut program
libertybasiccom.proboards.com/thread/2482/ascii-3d-spining-donut
I tried to move local parameters in nested functions to global variable
But it made program slower!
So mere
*using global variable*
is SLOWER then
* assigning local variable
and using *that* *
I really expected using global variable SAVE assignment time (done once, up front).
Especially the assignment time in LB4 is about 10x time of math operator, try this program and see
justbasiccom.proboards.com/thread/901/benchmarking-fast-different-stuff-jb
This behavior (using global is slower) is same in LB5.
I would say it is weird and counter intuitive as for trying to optimize program.
I really wonder if there is something in LB code that could be fixed regarding to this.
May be LB5 could benefit from it.
Here is the demo, number is function calls per second (more == faster).
Variant with globals is slowest.
I really expected it to be about as fast as last line (not using assignment, instead use preassigned (local) variable (again))
libertybasiccom.proboards.com/thread/2482/ascii-3d-spining-donut
I tried to move local parameters in nested functions to global variable
But it made program slower!
So mere
*using global variable*
is SLOWER then
* assigning local variable
and using *that* *
I really expected using global variable SAVE assignment time (done once, up front).
Especially the assignment time in LB4 is about 10x time of math operator, try this program and see
justbasiccom.proboards.com/thread/901/benchmarking-fast-different-stuff-jb
This behavior (using global is slower) is same in LB5.
I would say it is weird and counter intuitive as for trying to optimize program.
I really wonder if there is something in LB code that could be fixed regarding to this.
May be LB5 could benefit from it.
Here is the demo, number is function calls per second (more == faster).
Variant with globals is slowest.
I really expected it to be about as fast as last line (not using assignment, instead use preassigned (local) variable (again))
pass data as param
k= 43187
pass data as const
k= 42962
using local variable to use 0.001
k= 43494
using constant to use 0.001
k= 43389
using global variable to use 0.001
k= 42167
not using 0.001
k= 43966
'time testing
global p4
p4=0.001
gosub [waitSecondEdge]
'so how many function calls per second?
t0=time$("seconds")
k=0
p1=0.001
while t0=time$("seconds")
'scan
y = f1(p1)
k=k+1
wend
print "pass data as param"
print "k= ";k
gosub [waitSecondEdge]
t0=time$("seconds")
k=0
while t0=time$("seconds")
'scan
y = f1(0.001)
k=k+1
wend
print "pass data as const"
print "k= ";k
gosub [waitSecondEdge]
t0=time$("seconds")
k=0
while t0=time$("seconds")
'scan
y = f2()
k=k+1
wend
print "using local variable to use 0.001"
print "k= ";k
gosub [waitSecondEdge]
t0=time$("seconds")
k=0
while t0=time$("seconds")
'scan
y = f3()
k=k+1
wend
print "using constant to use 0.001"
print "k= ";k
gosub [waitSecondEdge]
t0=time$("seconds")
k=0
while t0=time$("seconds")
'scan
y = f4()
k=k+1
wend
print "using global variable to use 0.001"
print "k= ";k
gosub [waitSecondEdge]
t0=time$("seconds")
k=0
while t0=time$("seconds")
'scan
y = f5()
k=k+1
wend
print "not using 0.001"
print "k= ";k
end '---------------------------------------
'set time on the end of second
[waitSecondEdge]
t0=time$("seconds")
while t0=time$("seconds")
scan
wend
return
'---------------------------------------
function f1(p1) 'using param to pass value 0.001
x=1.2
f1=p1+x
end function
function f2() 'using local variable to use 0.001
p2=0.001
x=1.2
f2=p2+x
end function
function f3() 'using constant to use 0.001
x=1.2
f3=0.001+x
end function
function f4() 'using global variable to use 0.001
x=1.2
f4=p4+x
end function
function f5() 'NOT using 0.001
x=1.2
f5=x+x
end function