Just wondering which is faster, a Select/Case or If/ElseIf, After the program is compiled.
Thanks
Select/Case vs If/ElseIf
-
- Enthusiast
- Posts: 202
- Joined: Sun Apr 27, 2003 4:44 am
- Location: Michigan, USA
- Contact:
Select/Case vs If/ElseIf
-Ryan
RJP Computing
Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
RJP Computing
Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
-
- Enthusiast
- Posts: 202
- Joined: Sun Apr 27, 2003 4:44 am
- Location: Michigan, USA
- Contact:
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
RJP Computing
Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
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.
Anyway: Select/Case has one additional command -> PUSH [v_a] .
So don´t worry about that, it´s not worth it!
Fenix
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
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
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.
How much would it speed up as it´s only 3 commands for every case/if (mov,cmp,jcc)??
Fenix
- tinman
- PureBasic Expert
- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)