here's a vey simple test that shows that sub is faster than function
compile command: FreeBASIC-1.06.0-win64\fbc -O 2 -asm intel -w all -v foobar.bas
foobar.bas
Code: Select all
type bar
as double d
as long l
as longint ld
as zstring*19 sz
end type
function foo naked () as bar
asm
fldpi
fstp qword ptr [rcx]
mov eax, 123
mov dword ptr [rcx+8],eax
mov rax,123456789
mov qword ptr [rcx+16],rax
mov rax,.L0
mov qword ptr [rcx+24],rax
mov rax,.L0+8
mov qword ptr [rcx+32],rax
mov rax, rcx
ret
.L0: .asciz "hello world\n"
end asm
end function
function foofb () as bar
return type <bar>(3.141592653589793,123, 123456789, !"hello world\n")
end function
sub foo2 naked (byref b as bar)
asm
fldpi
fstp qword ptr [rcx]
mov eax, 123
mov dword ptr [rcx+8],eax
mov rax,123456789
mov qword ptr [rcx+16],rax
mov rax,.L1
mov qword ptr [rcx+24],rax
mov rax,.L1+8
mov qword ptr [rcx+32],rax
ret
.L1: .asciz "hello world\n"
end asm
end sub
sub foo2fb (byref b as bar)
b.d=3.141592653589793
b.l=123
b.ld=123456789
b.sz=!"hello world\n"
end sub
dim as bar y
dim as double t1, t2
t1=timer
for i as integer=1 to 100000000
y=foo()
next
t1=timer-t1
print "time for function naked = ";t1
print y.d, y.l, y.ld, y.sz
t2=timer
y.d=0
y.l=0
y.ld=0
y.sz=""
for i as integer=1 to 100000000
foo2(y)
next
t2=timer-t2
print "time for sub naked = ";t2
print "sub naked is ";t1/t2;" times faster than function naked"
print y.d, y.l, y.ld, y.sz
print
t1=timer
for i as integer=1 to 100000000
y=foofb()
next
t1=timer-t1
print "time for function fb = ";t1
print y.d, y.l, y.ld, y.sz
t2=timer
y.d=0
y.l=0
y.ld=0
y.sz=""
for i as integer=1 to 100000000
foo2fb(y)
next
t2=timer-t2
print "time for subfb = ";t2
print "subfb is ";t1/t2;" times faster than functionfb"
print y.d, y.l, y.ld, y.sz
time for function naked = 0.7277403000043705
3.141592653589793 123 123456789 hello world
time for sub naked = 0.2363193000201136
sub naked is 3.079478908165483 times faster than function naked
3.141592653589793 123 123456789 hello world
time for function fb = 3.426954100024886
3.141592653589793 123 123456789 hello world
time for subfb = 2.368696500023361
subfb is 1.44676791644311 times faster than functionfb
3.141592653589793 123 123456789 hello world