Select/Case vs If/ElseIf
Posted: Tue Jun 10, 2003 5:52 pm
Just wondering which is faster, a Select/Case or If/ElseIf, After the program is compiled.
Thanks
Thanks
http://www.purebasic.com
https://www.purebasic.fr/english/
I've asked Fred the same question and he told me it's equal, PB has a smart algorithm that avoids this types of slowdownsplouf wrote:Obviously select/case
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.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.
Code: Select all
a.l = 15
Select a
Case 1
Case 2
Default
EndSelect
If a=1
ElseIf a=2
Else
EndIf
End
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 :)Fenix wrote:tinman´s right. Select/Case and If/ElseIf have to do the same checks. No difference there.
tinman wrote:However, I hope that someday Fred can implement an optimisation to turn a select/case into a jump table if all "case" values are sequentialFenix wrote:tinman´s right. Select/Case and If/ElseIf have to do the same checks. No difference there.
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.Fenix wrote:tinman wrote:How much would it speed up as it´s only 3 commands for every case/if (mov,cmp,jcc)??Fenix wrote:tinman´s right. Select/Case and If/ElseIf have to do the same checks. No difference there.