Post by tsh73 on Oct 21, 2022 8:45:54 GMT
Rather, what operations cost what?
(then trying to make program run as fast as it could)
on a fast machine (Win 10 64bit):
0.0014 ms per iteration of empty FOR loop
700 000 iteration in a second
(I remember ZX Spectrum "5000 empty loops per second", so it was 0.2 ms per iteration )
Adding SCAN in a loop costs
0.00085 ms
The cost of assignment in a loop
about 0.001 ms
(integer assignment is about 1/2 faster)
Operation, say +, about 1/10 from assignment
looks like *,/ just about the same
except pow(^) what gives 3x of operation, so x*x is better then x^2
Adding single-line IF still cost less then assignment, about 1/2
Block IF about 0.8 of assignment
Using array item instead of variable adds 4x of operation
SQR is faster then (+) - so it surely wins over ()^0.5
SIN is about as fast as operation (but slower then SQR)
User-defined functions (UDF) is costly
UDF with 1 assignment inside (return value) cost as 9 assignments
passing each argument cost less then assignment
SUB is faster
calling empty sub cost like 3 assignments
passing each argument cost less then assignment (may be even 0.1, like operation)
Now graphics
SET cost 27 assignments
(26 with integer vars, 27 with int() around vars, 37 with floating point)
Line that draws singe point (100, 100-101, 100 because last point does not draws)
work in around same time, even faster then SET
Non-trivial line 100 100-300 200
28 assignments
I wonder if it depends of line length?
Indeed yes
line 100 100-1200, 900
41 assignments
Setting named color (color red)
19 assignments
Setting named color (color blue)
17 assignments
'---------------------------
Run and see.
Tweak and run.
Repeat until bored ;)
(don't forget that on other computer it could work differently)
(then trying to make program run as fast as it could)
on a fast machine (Win 10 64bit):
0.0014 ms per iteration of empty FOR loop
700 000 iteration in a second
(I remember ZX Spectrum "5000 empty loops per second", so it was 0.2 ms per iteration )
Adding SCAN in a loop costs
0.00085 ms
The cost of assignment in a loop
about 0.001 ms
(integer assignment is about 1/2 faster)
Operation, say +, about 1/10 from assignment
looks like *,/ just about the same
except pow(^) what gives 3x of operation, so x*x is better then x^2
Adding single-line IF still cost less then assignment, about 1/2
Block IF about 0.8 of assignment
Using array item instead of variable adds 4x of operation
SQR is faster then (+) - so it surely wins over ()^0.5
SIN is about as fast as operation (but slower then SQR)
User-defined functions (UDF) is costly
UDF with 1 assignment inside (return value) cost as 9 assignments
passing each argument cost less then assignment
SUB is faster
calling empty sub cost like 3 assignments
passing each argument cost less then assignment (may be even 0.1, like operation)
Now graphics
SET cost 27 assignments
(26 with integer vars, 27 with int() around vars, 37 with floating point)
Line that draws singe point (100, 100-101, 100 because last point does not draws)
work in around same time, even faster then SET
Non-trivial line 100 100-300 200
28 assignments
I wonder if it depends of line length?
Indeed yes
line 100 100-1200, 900
41 assignments
Setting named color (color red)
19 assignments
Setting named color (color blue)
17 assignments
'---------------------------
Run and see.
Tweak and run.
Repeat until bored ;)
(don't forget that on other computer it could work differently)
'N=1e6
N=100000
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
'scan
next
t1=time$("ms")
empLoop=t1-t0
print "Empty Loop"
print empLoop
print (empLoop)/N;" ms"
print
'------------------------------------
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
next
t1=time$("ms")
empLoopScan=t1-t0
print "Empty Loop with a SCAN"
print empLoopScan
print (empLoopScan)/N;" ms"
print "---------------------"
print "The cost of SCAN in a loop"
print (empLoopScan-empLoop)/N;" ms"
print
'------------------------------------
a=1.4
b=2.3
c=1.3
'a=1
'b=2
'c=3
'integer assign is about 1/2 from floating point
op$="assign"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=b
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
timeassign = loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-empLoopScan)/N;" ms"
print
'------------------------------------
'add is about 1/10 from assign
op$="add"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=b+c
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'if is about 1/2 from assign
tt=1 'true
op$="if x a=b+c"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
'if tt then a=b+c
'if tt=1 then a=b+c
if tt=1 then
a=b+c
end if
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
dim ar(10)
ar(3)=4.5
d=3
'add with array item
'using array item is +4x of simple add
op$="add with array"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=b+ar(d)
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'5 adds? Just like 5 times of single add
op$="5 adds"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=b+c+b+c+b+c
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'div is about +10% from add
op$="div"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=b/c
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'mul likely just as div
op$="mul"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=b*b
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'pow cost 4x of div/mul!!!
op$="pow"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=b^c
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'sqr cost less then add (???)
op$="sqr"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=sqr(b)
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'sin cost a bit more then sqr, +25%
op$="sin"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=sin(b)
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'UDF no params with 1 assign inside costs as 9 assigns
op$="UDF no params"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=f0()
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'UDF 1 param, about+0.5 assign
op$="UDF"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=f1(b)
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'UDF with 3args - +2 arg cost about 1 assign
op$="UDF 3 args"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
a=f2(b,c,d)
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'sub no params, About 3 assings
op$="SUB no params"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
call s0
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'------------------------------------
'sub 1 param, about+0.1 assign
op$="SUB 1 param"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
call s1 a
next
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
END 'comment to proceed to graphic part
'----------------------------------
'The cost of print x ;' ';y in a loop
'1.14595 ms
' I guess because if scroll
'The cost of drawing "#gr "; x ;" ";y in a loop
'0.02633 ms for integers
'0.0369 ms for floats
'0.02681 ms with INT() inside
' 27x from assignment
open "test" for graphics_nsb_nf as #gr
#gr "down"
x=100'+1/3
y=100'+1/3
op$="#gr 'set ';x ;' ';y"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
'print "set"; x ;" ";y
#gr "set "; x ;" ";y
'#gr "set "; int(x) ;" ";int(y)
next
close #gr
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'----------------------------------
'line single point
' 24x from assignment -a bit faster then set
open "test" for graphics_nsb_nf as #gr
#gr "down"
x=100
y=100
x2=101
y2=100
op$="#gr 'line single point"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
#gr "line "; x ;" ";y;" "; x2 ;" ";y2
next
close #gr
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'----------------------------------
'line 100 100-300 200
' 28x from assignment (+4 assignment from single point)
'open "test" for graphics_nsb_nf as #gr
open "test" for graphics_nsb_fs as #gr
#gr "down"
x=100
y=100
x2=1200
y2=900
op$="#gr 'line 100 100-300 200"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
#gr "line "; x ;" ";y;" "; x2 ;" ";y2
next
close #gr
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'----------------------------------
'color red
' 19x from assignment
open "test" for graphics_nsb_nf as #gr
#gr "down"
x=100
y=100
x2=300
y2=200
op$="#gr 'color red'"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
#gr "color red"
next
close #gr
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
'----------------------------------
'color 0 0 255
' 17x from assignment (faster then named color)
open "test" for graphics_nsb_nf as #gr
#gr "down"
x=100
y=100
x2=300
y2=200
op$="#gr 'color 0 0 255'"
t0=time$("ms")
while t0=time$("ms"):scan:wend
t0=time$("ms")
for i = 1 to N
scan
#gr "color 0 0 255"
next
close #gr
t1=time$("ms")
loopTime=t1-t0
print "Loop with a SCAN "+op$
print loopTime
print "---------------------"
print "The cost of "+op$+" in a loop"
print (loopTime-timeassign)/N;" ms"
print
function f0()
f1=1.1
end function
function f1(x)
f1=x
end function
function f2(x,y,z)
f1=x
end function
sub s0
end sub
sub s1 x
end sub