C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
yet another optimization thing Posted by Gregry2 on 3 Jul 2006 at 7:10 AM
pretend that a and b are arguments to a function containing this
int 
	l=a,
	s=b;
if(a<b){
	l=b;
	s=a;
}

/*alt*/

int l,s;
if(a>b){
	l=a;
	s=b;
}
else{
	l=b;
	s=a;
}

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
Report
Re: yet another optimization thing Posted by Lundin on 3 Jul 2006 at 7:32 AM
: pretend that a and b are arguments to a function containing this
:
: int 
: 	l=a,
: 	s=b;
: if(a<b){
: 	l=b;
: 	s=a;
: }
: 
: /*alt*/
: 
: int l,s;
: if(a>b){
: 	l=a;
: 	s=b;
: }
: else{
: 	l=b;
: 	s=a;
: }
: 

: 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.
Report
Re: yet another optimization thing Posted by AsmGuru62 on 5 Jul 2006 at 4:25 AM
This message was edited by AsmGuru62 at 2006-7-5 4:28:30

This message was edited by AsmGuru62 at 2006-7-5 4:25:46

: : pretend that a and b are arguments to a function containing this
: :
: : int 
: : 	l=a,
: : 	s=b;
: : if(a<b){
: : 	l=b;
: : 	s=a;
: : }
: : 
: : /*alt*/
: : 
: : int l,s;
: : if(a>b){
: : 	l=a;
: : 	s=b;
: : }
: : else{
: : 	l=b;
: : 	s=a;
: : }
: : 

: : 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.
:
Actually, the ELSE part of the IF() statement causes the additional JMP (forward) instruction, which is omitted if ELSE 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.





Report
Re: yet another optimization thing Posted by Lundin on 5 Jul 2006 at 5:34 AM
: This message was edited by AsmGuru62 at 2006-7-5 4:28:30

: This message was edited by AsmGuru62 at 2006-7-5 4:25:46

: : : pretend that a and b are arguments to a function containing this
: : :
: : : int 
: : : 	l=a,
: : : 	s=b;
: : : if(a<b){
: : : 	l=b;
: : : 	s=a;
: : : }
: : : 
: : : /*alt*/
: : : 
: : : int l,s;
: : : if(a>b){
: : : 	l=a;
: : : 	s=b;
: : : }
: : : else{
: : : 	l=b;
: : : 	s=a;
: : : }
: : : 

: : : 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.
: :
: Actually, the ELSE part of the IF() statement causes the additional JMP (forward) instruction, which is omitted if ELSE 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.

:
:


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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.