Author Topic: SUB or FUNCTION ?  (Read 4122 times)

9940

  • Guest
SUB or FUNCTION ?
« on: 2009-Jul-20 »
Which is better ?
Code: (glbasic) [Select]
WHILE TRUE
    DrawSomething()
    SHOWSCREEN
WEND
Code: (glbasic) [Select]
WHILE TRUE
    GOSUB DrawSomething
    SHOWSCREEN
WEND

I'm talking about performance

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1791
    • View Profile
    • Homepage
Re: SUB or FUNCTION ?
« Reply #1 on: 2009-Jul-20 »
After testing this in a loop of 1 000 000 iterations, GOSUB is slightly slower, about 50 ms slower on total time which gives about 1.5 % difference.

My function and sub both draws a sprite and then exits.

Offline trucidare

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1377
  • Bachelor of Fail
    • View Profile
Re: SUB or FUNCTION ?
« Reply #2 on: 2009-Jul-20 »
Internal SUB = function without params. No more
MacBook Pro 2,2 GHz Core 2 Duo, 4 GB RAM, 160 GB HDD, 8600M GT
Core i3 - 3,07 GHz, 8 GB Ram, 2.5 TB HDD, Geforce GTX 260+ OC

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
Re: SUB or FUNCTION ?
« Reply #3 on: 2009-Jul-21 »

My function and sub both draws a sprite and then exits.


Try your test with math instead of calls to the gpu.

Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1791
    • View Profile
    • Homepage
Re: SUB or FUNCTION ?
« Reply #4 on: 2009-Jul-21 »

My function and sub both draws a sprite and then exits.


Try your test with math instead of calls to the gpu.



Was the same result so I tried with sprites too.

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
Re: SUB or FUNCTION ?
« Reply #5 on: 2009-Jul-21 »
Strange, there shouldnt be a performance difference, if sub is merely a function.

Out of curiosity, im going to test this also, ill be back here with the results today.
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

9940

  • Guest
Re: SUB or FUNCTION ?
« Reply #6 on: 2009-Jul-21 »
Code: (glbasic) [Select]
FUNCTION nothing:
ENDFUNCTION

SUB nothing2:
ENDSUB

become

Code: (glbasic) [Select]
DGInt nothing()
{
   __PPRegisterFunction
return 0;
}
// ------------------------ //
DGInt nothing2(void){
   __PPRegisterFunction
return 0;
}

So there is no difference...

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1791
    • View Profile
    • Homepage
Re: SUB or FUNCTION ?
« Reply #7 on: 2009-Jul-22 »
Do a test, mabe my test is flawed in some way? There isn't a bit difference so is within error tollerances I guess.

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
Re: SUB or FUNCTION ?
« Reply #8 on: 2009-Jul-22 »
Do a test, mabe my test is flawed in some way?

Nah i highly doubt it. Youre usually the one to catch these subtlties in the first place, this number bloke has more time than you haha.

And ocean is probably on target with the cause.


Personally i never used the subs.
I remember when "GOSUB" was added, it was actually an idea i had a few years ago, to marry the "old" BASIC structure to "GLBasic" as an innate ability. Its really just a sentimental asthetic anymore.

"Function" is now obviously faster, and naturally, more versatile with the ability to byref and return etc.
I would definetly recommend function.

Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)