Select/Case vs If/ElseIf

Just starting out? Need help? Post your questions and find answers here.
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Select/Case vs If/ElseIf

Post by RJP Computing »

Just wondering which is faster, a Select/Case or If/ElseIf, After the program is compiled.

Thanks
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
plouf
Enthusiast
Enthusiast
Posts: 281
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Post by plouf »

Obviously select/case
Christos
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

plouf wrote:Obviously select/case
I've asked Fred the same question and he told me it's equal, PB has a smart algorithm that avoids this types of slowdowns :lol:
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

I only aked because the new Visual Designer makes a SAMPLE event loop out of if/elseif and I know in other languages this is slower because it must go through all elseif's. Just wondering.
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

RJP Computing wrote:I only aked because the new Visual Designer makes a SAMPLE event loop out of if/elseif and I know in other languages this is slower because it must go through all elseif's. Just wondering.
Last time I checked (maybe 3.51?) PureBasic also goes through each Case until it finds the matching one. Hopefully it might one day it might be able to make jump tables in some cases to speed it up, but for now your best bet is to put the most commonly used block of code as the first block in your if or select loop.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Fenix
Enthusiast
Enthusiast
Posts: 102
Joined: Wed May 07, 2003 1:45 am
Location: Germany

Post by Fenix »

tinman´s right. Select/Case and If/ElseIf have to do the same checks. No difference there.

Compile following code using /COMMENTED flag and view the assembler source.

Code: Select all

a.l = 15

Select a
   Case 1
   Case 2
   Default
EndSelect


If a=1
ElseIf a=2
Else
EndIf

End

Anyway: Select/Case has one additional command -> PUSH [v_a] .
So don´t worry about that, it´s not worth it!


Fenix
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Fenix wrote:tinman´s right. Select/Case and If/ElseIf have to do the same checks. No difference there.
However, I hope that someday Fred can implement an optimisation to turn a select/case into a jump table if all "case" values are sequential :)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Fenix
Enthusiast
Enthusiast
Posts: 102
Joined: Wed May 07, 2003 1:45 am
Location: Germany

Post by Fenix »

tinman wrote:
Fenix wrote:tinman´s right. Select/Case and If/ElseIf have to do the same checks. No difference there.
However, I hope that someday Fred can implement an optimisation to turn a select/case into a jump table if all "case" values are sequential :)

How much would it speed up as it´s only 3 commands for every case/if (mov,cmp,jcc)??


Fenix
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Fenix wrote:
tinman wrote:
Fenix wrote:tinman´s right. Select/Case and If/ElseIf have to do the same checks. No difference there.
How much would it speed up as it´s only 3 commands for every case/if (mov,cmp,jcc)??
It would depend on how many items you had, and how often you would need to go far into that list. I think with a jump table you should only need 4 or 5 instructions (estimate: I am no x86 asm expert) to reach any case block.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Fenix
Enthusiast
Enthusiast
Posts: 102
Joined: Wed May 07, 2003 1:45 am
Location: Germany

Post by Fenix »

I got your point - that might come in handy.
It could also reduce the final code size, as you only need 4 bytes (long) for each case/if.

;) Even better: it´s Fred who has to implement it ;)



Fenix
Post Reply