1. if statement optimization
- Posted by frankied Oct 28, 2008
- 1014 views
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
?
2. Re: if statement optimization
- Posted by jeremy (admin) Oct 28, 2008
- 1042 views
I setup the example:
constant COUNT = 100000000 constant HALF_COUNT = COUNT/2 atom t1 = time() function case_one(integer i) if i > HALF_COUNT then return i end if return i end function function case_two(integer i) if i > HALF_COUNT then return i else return i end if end function integer a = 1 for i = 1 to COUNT do a = case_one(i) end for puts(1, "Case One: ") ? time() - t1 t1 = time() for i = 1 to COUNT do a = case_two(i) end for puts(1, "Case Two: ") ? time() - t1
and when running, I get:
C:\Users\Jeremy\Desktop>exwc test.ex Case One: 3.198 Case Two: 3.200 C:\Users\Jeremy\Desktop>exwc test.ex Case One: 3.198 Case Two: 3.198 C:\Users\Jeremy\Desktop>exwc test.ex Case One: 3.197 Case Two: 3.198
Jeremy
3. Re: if statement optimization
- Posted by mattlewis (admin) Oct 28, 2008
- 1033 views
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
4. Re: if statement optimization
- Posted by frankied Oct 28, 2008
- 1018 views
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
5. Re: if statement optimization
- Posted by Spock Oct 28, 2008
- 951 views
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
?
There is also:
return not (condition)
6. Re: if statement optimization
- Posted by unkmar Nov 11, 2008
- 924 views
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
The code below will cause me to want to kick your teeth in.
Thankfully, our geographical dispersion and my usual kind nature would prevent that.
if (condition) then -- any amount of code return else -- lots of code return end if -- I prefer object result if (condition) then -- any amount of code result = "TRUE" else -- lots of code result = "FALSE" end if -- print(LOGFILE, result) -- Thus allowing for easy debugging. return result
Unkmar = "Lucius L. Hilley III"