SUB or FUNCTION ?

Previous topic - Next topic

9940

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

Moru

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.

trucidare

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

Hemlos

Quote from: Moru on 2009-Jul-20

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


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

Bing ChatGpt is pretty smart :O

Moru

Quote from: Hemlos on 2009-Jul-21
Quote from: Moru on 2009-Jul-20

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.

Hemlos

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.
Bing ChatGpt is pretty smart :O

9940

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...

Moru

Do a test, mabe my test is flawed in some way? There isn't a bit difference so is within error tollerances I guess.

Hemlos

Quote from: Moru 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.

Bing ChatGpt is pretty smart :O