Afficher un message
Vieux 16/10/2007, 07h39   #2
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does 'if' have performance overhead

lali.b97@gmail.com wrote:

> Somewhere in a tutorial i read that if statement has performance
> overheads as code within the if statement cannot take benefit of
> pipeling of microprocessor and also that the compiler cannot
> agressively optimize that code.


Somewhat true, I hear.

It's not the if-statement per se. It's the fact that control flow of the
program branches.


> From that day onwards i have been trying to avoid if statement withing
> my functions as much as possible and also try to have minimum code
> withing if block.


That is a Bad Idea(tm).

Optimizations avoiding if-statements are usually non-obvious and change the
nature of your code fundamentally. To see that, just try to rewrite

unsigned int max ( unsigned int lhs, unsigned int rhs );

without branch statements. Generally, avoiding branch statements can be
considered code obfuscation. Code obfuscation for the sake of performance
gains that have not proven necessary by profiling is a form of premature
optimization.


> However, i am bit skeptic about this.


What do your measurements tell you?


> I need some guidance. Performance is always the key issue for me when
> it comes to writing programs.


It should not be. Programmer time is much more expensive than CPU time. The
rational choice is to optimize for code beauty, extendability, ease of use,
and maintenability.


Best

Kai-Uwe Bux
  Réponse avec citation
 
Page generated in 0,06693 seconds with 9 queries