Page 1 of 1

Select/Case vs If/ElseIf

Posted: Tue Jun 10, 2003 5:52 pm
by RJP Computing
Just wondering which is faster, a Select/Case or If/ElseIf, After the program is compiled.

Thanks

Posted: Tue Jun 10, 2003 9:01 pm
by plouf
Obviously select/case

Posted: Tue Jun 10, 2003 9:40 pm
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:

Posted: Tue Jun 10, 2003 10:57 pm
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.

Posted: Tue Jun 10, 2003 11:22 pm
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.

Posted: Wed Jun 18, 2003 11:41 pm
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

Posted: Thu Jun 19, 2003 12:05 am
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 :)

Posted: Thu Jun 19, 2003 12:22 am
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

Posted: Thu Jun 19, 2003 12:34 am
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.

Posted: Thu Jun 19, 2003 12:45 am
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