#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# yet another optimization thing

Posts: 606Member
pretend that a and b are arguments to a function containing this
[code]
int
l=a,
s=b;
if(ab){
l=a;
s=b;
}
else{
l=b;
s=a;
}
[/code]
There is no difference between the two, only the former is more unclear about what the programmer expects, it seems that a is expected to be more and b, less, and the elimination of the else doesn't really matter, right?

thats all.
{2}rIng
· ·

• Posts: 3,711Member
: pretend that a and b are arguments to a function containing this
: [code]
: int
: l=a,
: s=b;
: if(ab){
: l=a;
: s=b;
: }
: else{
: l=b;
: s=a;
: }
: [/code]
: There is no difference between the two, only the former is more unclear about what the programmer expects, it seems that a is expected to be more and b, less, and the elimination of the else doesn't really matter, right?
:
: thats all.
: {2}rIng
:

The first version assigns values to the same variable twice, which looks messy imo. It is also less effective, so use the second version.
· ·
• Posts: 6,519Member
[b][red]This message was edited by AsmGuru62 at 2006-7-5 4:28:30[/red][/b][hr]
[b][red]This message was edited by AsmGuru62 at 2006-7-5 4:25:46[/red][/b][hr]
: : pretend that a and b are arguments to a function containing this
: : [code]
: : int
: : l=a,
: : s=b;
: : if(ab){
: : l=a;
: : s=b;
: : }
: : else{
: : l=b;
: : s=a;
: : }
: : [/code]
: : There is no difference between the two, only the former is more unclear about what the programmer expects, it seems that a is expected to be more and b, less, and the elimination of the else doesn't really matter, right?
: :
: : thats all.
: : {2}rIng
: :
:
:
: The first version assigns values to the same variable twice, which looks messy imo. It is also less effective, so use the second version.
:
[blue]Actually, the [b]ELSE[/b] part of the IF() statement causes the additional JMP (forward) instruction, which is omitted if [b]ELSE[/b] is not present. So, I will not be too quick to judge that one. Best bet is to assign (inside IF()) the values, which are the most frequent case in that piece of code.

Jumps forward in code are not predicted by CPU and therefore should be used only when needed. I am nitpicking, of course, because the difference in execution time will be visible only on a VERY, VERY large loops.[/blue]

· ·
• Posts: 3,711Member
: [b][red]This message was edited by AsmGuru62 at 2006-7-5 4:28:30[/red][/b][hr]
: [b][red]This message was edited by AsmGuru62 at 2006-7-5 4:25:46[/red][/b][hr]
: : : pretend that a and b are arguments to a function containing this
: : : [code]
: : : int
: : : l=a,
: : : s=b;
: : : if(ab){
: : : l=a;
: : : s=b;
: : : }
: : : else{
: : : l=b;
: : : s=a;
: : : }
: : : [/code]
: : : There is no difference between the two, only the former is more unclear about what the programmer expects, it seems that a is expected to be more and b, less, and the elimination of the else doesn't really matter, right?
: : :
: : : thats all.
: : : {2}rIng
: : :
: :
: :
: : The first version assigns values to the same variable twice, which looks messy imo. It is also less effective, so use the second version.
: :
: [blue]Actually, the [b]ELSE[/b] part of the IF() statement causes the additional JMP (forward) instruction, which is omitted if [b]ELSE[/b] is not present. So, I will not be too quick to judge that one. Best bet is to assign (inside IF()) the values, which are the most frequent case in that piece of code.
:
: Jumps forward in code are not predicted by CPU and therefore should be used only when needed. I am nitpicking, of course, because the difference in execution time will be visible only on a VERY, VERY large loops.[/blue]
:
:

Just moving two int (which can be 16 or 32 bit) to another memory location won't take long, but it is CPU dependant. On an 8-bit CPU it will take several instructions and therefore be slower than some additional jumps.

But since we are just talking about a few CPU ticks more or less, choose the version that is most readable/intuitive, which in this case is the if-else version.
· ·