Re: if statement optimization
- Posted by mattlewis (admin) Oct 28, 2008
- 1032 views
frankied said...
In a tight loop, is there any performance difference betweeen:
if (condition) then return 0 else return 1 end if
and:
if (condition) then return 0 end if return 1
As Jeremy showed, there is very little difference. The only real difference is that the second return ends up a little bit farther away (in memory) in the first case, which could possibly account for the minute difference that his test showed. Here is the IL for the two:
1: 020 135 9 # IF: [condition:135] = 0 goto 0009 4: 028 136 137 # RETURNF: [LIT 0:137] 7: 023 12 # ELSE goto 0012 9: 028 136 133 # RETURNF: [LIT 1:133] 12: 020 135 18 # IF: [condition:135] = 0 goto 0018 15: 028 136 137 # RETURNF: [LIT 0:137] 18: 028 136 133 # RETURNF: [LIT 1:133]Matt