hey, is this code:

if(m_velocity.x == 0)
		return 0;
	return ToDegrees(atan(m_velocity.y/m_velocity.x));

any more efficient than this code:

if(m_velocity.x == 0)
		return 0;
else
	return ToDegrees(atan(m_velocity.y/m_velocity.x));

I.e., are we saving any time by omitting the else statement?

No.

(If you want to get technical, it depends on the quality of the compiler. But the most difference it will cause is a couple cycles, which is infinitesimal.)

I never use the else in a statement like that. But there really is nothing wrong with it because a good optimizing compiler will probably delete the else anyway.

How about

if(m_velocity.x == 0) {
	result = 0;
} else {
	result = ToDegrees(atan(m_velocity.y/m_velocity.x));
}
return result;

Seconds spent over-optimising what any half-decent compiler will manage to do just fine can save days of debugging time later on.

Sure, the code looks just like that now, but a few edits later and a mental slip-up and the code is off in la-la land doing something you'll spend ages figuring out just to go "D'OH" when you eventually find it.

Of course, the next question is "Is m_velocity.x an integer or a float?"
It it's a float, why are you comparing it with zero?
http://c-faq.com/fp/fpequal.html

hi coolgamer48,

I let the compiler generate the assembly code of a small program containing if with/without else path to figure out whether omitting else would affect execution time, results below:

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{  
//  Without else
	if ( argc == 0 ) return 0;
	return 1;

//  With else
	if ( argc == 0 ) return 0;
	else return 1;
}

/***************   Generated assembly output by VS 2005, V8.0.5
;;;; four semicolons indicate comments added by myself

;;;;  1st Example without else: if ( argc == 0 ) return 0; return 1;
;;;; Entry code left out
 
; 8    : //  Without else
; 9    : 	if ( argc == 0 ) return 0;

	cmp	DWORD PTR _argc$[ebp], 0
	jne	SHORT $LN1@wmain
	xor	eax, eax
	jmp	SHORT $LN2@wmain
$LN1@wmain:

; 10   : 	return 1;

	mov	eax, 1
$LN2@wmain:   ;;;; code to return to caller left out

;;;;  2nd Example with else: if ( argc == 0 ) return 0; else return 1;
;;;; Entry code left out

; 8    : //  With else
; 9    : 	if ( argc == 0 ) return 0;

	cmp	DWORD PTR _argc$[ebp], 0
	jne	SHORT $LN2@wmain
	xor	eax, eax
	jmp	SHORT $LN3@wmain
	jmp	SHORT $LN3@wmain    ;;;; <-- Doubly generated (dead) code
$LN2@wmain:

; 10   : 	else return 1;

	mov	eax, 1
$LN3@wmain:   ;;;; code to return to caller left out
***************/

Above small C++ program has been compiled with option to generate assembly file (.asm). Comparison of the generated assembly instructions of 1st and 2nd example shows no differences between if statement without else and if statement with else path. (Apart from the fact that such famous c++ compiler also generates dead code even so for such simple c++ coding, what may also be the reason for different label numbering ($LN2 vs $LN3). Dead code and different label numbering absolutely do not affect any performance. For above simple if-example omitting else path does not save time.

krs,
cliff

Try that with optimisation enabled, and the dead code should go away.

Try that with optimisation enabled, and the dead code should go away.

Thank you Salem,
if I enable optimisation compilation error occurs: command line options /Ox and /RTC1 were incompatible, it says so. Occurs also with /O1 or /O2. Shall I switch off run time check?

If you want to optimise, yes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.