timing sub vs function

General FreeBASIC programming questions.
srvaldez
Posts: 1996
Joined: Sep 25, 2005 21:54

timing sub vs function

Postby srvaldez » May 29, 2019 17:47

for Windows x64
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
Last edited by srvaldez on May 30, 2019 12:25, edited 1 time in total.
fxm
Posts: 9010
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: timing sub vs function

Postby fxm » May 29, 2019 18:52

You compare things that are not comparable:
- An object is passed by reference to the Sub.
- The Function returns an object by value.

Example (for FB code only) with a Function which returns by reference:

Code: Select all

type bar
   as double d
   as long l
   as longint ld
   as zstring*19 sz
end type

function foofb () byref as bar
   Static as bar b
   b.d=3.141592653589793
   b.l=123
   b.ld=123456789
   b.sz=!"hello world\n"
   return b
end function

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 ptr py
dim as bar y
dim as double t1, t2

t1=timer
for i as integer=1 to 100000000
   py = @(foofb())
next
t1=timer-t1
print "time for function fb = ";t1
print py->d, py->l, py->ld, py->sz

y.d=0
y.l=0
y.ld=0
y.sz=""
t2=timer
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
srvaldez
Posts: 1996
Joined: Sep 25, 2005 21:54

Re: timing sub vs function

Postby srvaldez » May 29, 2019 18:58

thanks fxm, your version makes the times about equal, perhaps a better example would be a function/sub that modifies the values passed

Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests