473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Which is more efficient?

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

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

OR

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

for (i=0; i<16; ++i)
if ((fTemp=faArray[i]-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.

Sep 28 '07 #1
4 1123
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
Sep 28 '07 #2
PeterOut wrote:
float fMax=0, faArray[16], fValue;
int i;

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

OR

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

for (i=0; i<16; ++i)
if ((fTemp=faArray[i]-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
Sep 28 '07 #3
PeterOut wrote:
float fMax=0, faArray[16], fValue;
int i;

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

OR

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

for (i=0; i<16; ++i)
if ((fTemp=faArray[i]-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.
Sep 28 '07 #4
On Fri, 28 Sep 2007 21:27:06 +0530, santosh wrote:
PeterOut wrote:
>float fMax=0, faArray[16], fValue;
int i;

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

OR

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

for (i=0; i<16; ++i)
if ((fTemp=faArray[i]-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.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: wogston | last post by:
(A) template <typename T> metainline foo<T> lerp(const foo<T>& a, const foo<T>& b, const T& time) { foo<T> v; repeat< 4,exec_mul<T> >::exec(v,a,b,time); return v; }
9
by: Randell D. | last post by:
Folks, I have a large amount of values to store (we're talking tens, if not hundreds of bytes). I need this for a client side application - ignore the security consequences for the moment -...
10
by: Amit | last post by:
Which is more efficient and why? p++ or ++p. Thanks.
12
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
1
by: JJ | last post by:
I need to do a SELECT query that joins four tables. The largest of the tables could eventually have over 1 million records. I need to do a SELECT that returns a maximum of 1000 records which will...
16
by: Dustan | last post by:
I have a program that uses up a lot of CPU and want to make it is efficient as possible with what I have to work with it. So which of the following would be more efficient, knowing that l is a list...
2
theGeek
by: theGeek | last post by:
I always wonder which one of join or subquery should I be using to solve a particuar problem so that I get the correct result set in minimum time. It usually happens that I can write a query quickly...
19
by: santanu mishra | last post by:
Hi , I am stuck with a requirement from my client to change the date format from mm/dd/yy to dd/mm/yy .If any body can help me out with this regard as its very much urgent. Regards,...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
6
by: Christopher | last post by:
I've seen various ways of doing this, but what I want is to be able to take a base class pointer and know which derived class to cast to. For example I am going to make a base light class, that...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.