1. if statement optimization

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 

?

new topic     » topic index » view message » categorize

2. Re: if statement optimization

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: if statement optimization

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

new topic     » goto parent     » topic index » view message » categorize

4. Re: if statement optimization

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

new topic     » goto parent     » topic index » view message » categorize

5. Re: if statement optimization

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 

?

There is also:

return not (condition) 
new topic     » goto parent     » topic index » view message » categorize

6. Re: if statement optimization

frankied said...

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 

Peace out
Unkmar = "Lucius L. Hilley III"

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu