Speed function: if/else vs select

Previous topic - Next topic

Hark0

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

Asmodean

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.


Schranz0r

thats also my opinion -> SELECT
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

kanonet

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

kanonet

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

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.

Hark0

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

kanonet

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

Yes, certainly is.  Whilst CPU cycles for the offending line would be small, it isn't really needed as it would never get executed!