GLBasic forum

Main forum => GLBasic - en => Topic started by: Hark0 on 2013-Aug-24

Title: Speed function: if/else vs select
Post by: Hark0 on 2013-Aug-24
Hi!

Which function is faster?

if / else / endif

or

select

::)
Title: Re: Speed function: if/else vs select
Post by: Asmodean on 2013-Aug-24
I hope I'm not totally wrong, but I mean that Select-Case can be optimized by the compiler to something like a branchtable. So I think Select Case will be faster then If-Then-Else.

Title: Re: Speed function: if/else vs select
Post by: Schranz0r on 2013-Aug-24
thats also my opinion -> SELECT
Title: Re: Speed function: if/else vs select
Post by: kanonet on 2013-Aug-24
Select can be optimisedin some special cases (it is its faster then if) - besides this special cases it should be the same speed like if. But you never exactly know what kind of optimisations your compiler does and what not.
Title: Re: Speed function: if/else vs select
Post by: kanonet on 2013-Aug-24
But there is no guaranty that results would be the same on other machines, most times its not really useful to optimise for your own machine - you dont really know what will happen on your customers machines.
Title: Re: Speed function: if/else vs select
Post by: MrTAToad on 2013-Aug-24
Select/case is (currently) always converted to groups of if statements.  IF statements always adds a "if (false)" block to the C++ code as well for some reason.  I have mentioned that to Gernot before as it's not needed and just wastes processor time.
Title: Re: Speed function: if/else vs select
Post by: Hark0 on 2013-Aug-25
Hi guys, I think the same.... C++ compiled SELECT are in theory more fast...

But, last comment of @MrTaToad have turned me crazy  O_O

select are converted to a if / then?

:blink:
Title: Re: Speed function: if/else vs select
Post by: kanonet on 2013-Aug-25
Yes our theory was one point - but MrT is right GLB does compile to if (with the unnecessary if (false)) that he mentioned. TY for pointing this out, I will avoid select for now.

Its always interesting to have a look at the generated code. ;)
Title: Re: Speed function: if/else vs select
Post by: MrTAToad on 2013-Aug-25
Yes, certainly is.  Whilst CPU cycles for the offending line would be small, it isn't really needed as it would never get executed!