Afficher un message
Vieux 16/10/2007, 08h46   #3
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does 'if' have performance overhead

<lali.b97@gmail.com> wrote in message
news:1192512322.602459.305500@q3g2000prf.googlegro ups.com...
> 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.
>
>>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.
>
> However, i am bit skeptic about this.
>
> I need some guidance. Performance is always the key issue for me when
> it comes to writing programs.
>
> Please guide.


You should not prematurely optimize. That is, don't attempt to optimize
code until you find what is actually taking the time.

if statements are extremly common in code in all languages They essentially
come down to a jump in the CPU based on some condition (jump if not zero,
jump if greater than zero, etc...). Now, I believe you are talking about
prefetching instructions and that he CPU won't know what set of instructions
to prefetch if there is a jump, set A or set B.

A lot of times the compiler itself may optimize the code, and a lot of times
the CPUs are smart enough to either figure it out or prefetch both branches.
In other words, I would not worry about if statments taking up too much
time. Although I wouldn't throw in if statments for no reason either.


  Réponse avec citation
 
Page generated in 0,05566 seconds with 9 queries