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 Programmer's 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 its 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

Gregry2Gregry2 Posts: 607Member
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

Comments

  • LundinLundin 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.
  • AsmGuru62AsmGuru62 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]




  • LundinLundin 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.
Sign In or Register to comment.