Author Topic: Speed function: if/else vs select  (Read 1390 times)

Offline Hark0

  • Prof. Inline
  • *****
  • Posts: 1020
  • Geek Developer
    • View Profile
    • LitioPixel - Desarrollo de videojuegos con GLBasic | Videogame development with GLBasic
Speed function: if/else vs select
« on: 2013-Aug-24 »
Hi!

Which function is faster?

if / else / endif

or

select

 ::)
http://litiopixel.blogspot.com
litiopixel.blogspot.com - Desarrollo videojuegos Indie · Pixel-Art · Retroinformática · Electrónica Development Indie Videogames · Pixel-Art · Retrocomputing · Electronic

Offline Asmodean

  • Mc. Print
  • *
  • Posts: 29
    • View Profile
Re: Speed function: if/else vs select
« Reply #1 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.


Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5014
  • O Rly?
    • View Profile
Re: Speed function: if/else vs select
« Reply #2 on: 2013-Aug-24 »
thats also my opinion -> SELECT
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Speed function: if/else vs select
« Reply #3 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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Speed function: if/else vs select
« Reply #4 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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

MrTAToad

  • Guest
Re: Speed function: if/else vs select
« Reply #5 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.

Offline Hark0

  • Prof. Inline
  • *****
  • Posts: 1020
  • Geek Developer
    • View Profile
    • LitioPixel - Desarrollo de videojuegos con GLBasic | Videogame development with GLBasic
Re: Speed function: if/else vs select
« Reply #6 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:
http://litiopixel.blogspot.com
litiopixel.blogspot.com - Desarrollo videojuegos Indie · Pixel-Art · Retroinformática · Electrónica Development Indie Videogames · Pixel-Art · Retrocomputing · Electronic

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Speed function: if/else vs select
« Reply #7 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. ;)
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

MrTAToad

  • Guest
Re: Speed function: if/else vs select
« Reply #8 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!