Re: if statement optimization
- Posted by frankied Oct 28, 2008
- 1017 views
mattlewis said...
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
If you are just using it in general programming, what is accepted as the best programming practice? eg:
if (condition) then -- do a really big operation here else -- do a different really big operation here end if
or
if (condition) then -- do a really big operation here return end if -- do a different really big operation here return
or is it accepted as purely a matter of personal style