Which is more efficient?

P

PeterOut

float fMax=0, faArray[16], fValue;
int i;

for (i=0; i<16; ++i)
if ((faArray-fValue)>fMax) fMax=faArray-fValue;

OR

float fMax=0, faArray[16], fValue, fTemp;
int i;

for (i=0; i<16; ++i)
if ((fTemp=faArray-fValue)>fMax) fMax=fTemp;

In the former case I am doing a subtraction twice but only if the
condition is true. In the latter case, I am writing the difference
into a variable that may not be used, depending on the condition.

Many thanks in advance,
Peter.
 
W

Willem

PeterOut wrote:
) <snip: which is more efficient>
) In the former case I am doing a subtraction twice but only if the
) condition is true. In the latter case, I am writing the difference
) into a variable that may not be used, depending on the condition.

The canonical answer: Either. Both. Neither. It depends.

A more informed answer: most compilers will be smart enough to remember
the result of the expression so they don't have to calculate it twice.
However, lots of compilers will probably emit the exact same machine
codes for either of the two pieces of code.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
J

jacob navia

PeterOut said:
float fMax=0, faArray[16], fValue;
int i;

for (i=0; i<16; ++i)
if ((faArray-fValue)>fMax) fMax=faArray-fValue;

OR

float fMax=0, faArray[16], fValue, fTemp;
int i;

for (i=0; i<16; ++i)
if ((fTemp=faArray-fValue)>fMax) fMax=fTemp;

In the former case I am doing a subtraction twice but only if the
condition is true. In the latter case, I am writing the difference
into a variable that may not be used, depending on the condition.

Many thanks in advance,
Peter.


This question is impossible to answer without more information:

Which machine?
Which OS?
Which compiler?

In general the only answer to this questions can be found by

*MEASURING*

the differences yourself. After that, instead of just making
guesses, you will have hard data about your problem.

Then, you will notice that this doesn't make any difference at all
and that 99% of the time your program is doing something
completely different that you did not think about at all

*MEASURE*

Then you will know what you are talking about!

jacob
 
S

santosh

PeterOut said:
float fMax=0, faArray[16], fValue;
int i;

for (i=0; i<16; ++i)
if ((faArray-fValue)>fMax) fMax=faArray-fValue;

OR

float fMax=0, faArray[16], fValue, fTemp;
int i;

for (i=0; i<16; ++i)
if ((fTemp=faArray-fValue)>fMax) fMax=fTemp;

In the former case I am doing a subtraction twice but only if the
condition is true. In the latter case, I am writing the difference
into a variable that may not be used, depending on the condition.

Many thanks in advance,


It's impossible to say. An optimiser is likely to cache the subtraction and
reuse the value in the first case, which is what you're doing manually in
the second example. In any case, this is an example of microoptimisation
that I would not bother with. Just use the form that's most clear and
logical in relation to it's surrounding code.
 
A

Army1987

PeterOut said:
float fMax=0, faArray[16], fValue;
int i;

for (i=0; i<16; ++i)
if ((faArray-fValue)>fMax) fMax=faArray-fValue;

OR

float fMax=0, faArray[16], fValue, fTemp;
int i;

for (i=0; i<16; ++i)
if ((fTemp=faArray-fValue)>fMax) fMax=fTemp;

In the former case I am doing a subtraction twice but only if the
condition is true. In the latter case, I am writing the difference
into a variable that may not be used, depending on the condition.

It's impossible to say. An optimiser is likely to cache the subtraction
and reuse the value in the first case,
[...]
Note that, at least in C99, subtraction between floating point
numbers can have defined side effects in certain cases (e.g.
setting floating point status flags, or raising SIGFPE...)
I've noticed that on gcc dividing a double by 8.0 takes longer
than multiplying it by 0.125 (and yes, both 8.0 and 0.125 were
constants).
IOW, whereas on integers you can expect a compiler to do all the
sorts of micro-optimizations which could be useful, on floats you
can't be completely confident.

To the OP: measure, measure, measure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top