PDA

View Full Version : Using math expressions in C



achuthpnr
November 25th, 2013, 06:24 AM
I am wondering which is the preffered way of programming in C, if one is using the same math expressions very often
1. Use the math function/expression each time
2. Precalculate the expression and store it to a variable and use the variable instead

My questions are:
1. Will there be any difference in the execution time?
2. When a call is made to a variable to which the expression is stored, is it actually calculating the expression or just retrieving the value of that expression?

I hope the questions are clear.

Thanks.

Bachstelze
November 25th, 2013, 06:46 AM
The answers to your questions depend on the compiler used, and probably also the optimisation settings. Storing the result in a variable seems to be the best way to ensure the best performance, but it might hurt readability, especially if you name your variables poorly. Personally I would do it only if performance is critical, and even then only if it does indeed make a difference on the compiler used.

w.w.milner
November 25th, 2013, 10:09 AM
Answers to your questions
1. Yes
2. Just retrieving the value.

If you evaluate teh same expression often, you should make it a function ie
x= some expression;
y= some expression;
z = some expression;
the someexpression should be a function:
double f(double a, double b)
{
..
return result;
}
Since then you can
1. Do less typing
2. Test easier
3. Implement in a better way with little change.
For more effciency, uses memoization. You need to Google that. Basically whne you caluculate teh function, you store teh result as well. On a future call you look it up, not repeat teh calculation.