# Declaring temporary variable to save 1 multiply?

This topic is 1273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Really simply question, really. Is it worth declaring a temporary (float) variable to reduce an operation from 2 to 1 multiplies?

For example,

x = a * b / c;

y = a * b / d;

or..

float ab = a * b;

x = ab / c;

y = ab / d;

I'm not very familiar with assembly language, or how everything breaks down when its compiled, so that's why I'm asking. I'm not trying to optimize (really), I'm actually hoping to do the opposite (avoid the temporaries). If the function wasn't part of my math library, I probably wouldn't even bother worrying about it. The method I'm writing generates the (matrix-like) direction vectors of a quaternion. In the method, there are 2 of 9 unique multiplies, which means I could either multiply 18 times, or declare 9 variables and multiply 9 times.

I originally wrote the function by declaring the temporary variables, but it would look a lot nicer to simply do the math twice, as long as the difference is negligible.

Thanks a bunch for any advice

##### Share on other sites

Generally, if you have a large expression in which different parts of it meaningfully represent different values which make up the equation, then it makes sense to do this.

For example, it would be fruitless to do this when you're just doing some arbitrary operations. But if you have a formula which uses variables in it which you have to calculate, then it makes sense to abstract away these variables. In the distance formula:

d = sqrt((x2-x1)^2 + (y2-y1)^2)

(x2-x1) and (y2-y1) represent meaningful values: the x and y distance. Thus it would make sense to abstract the formula away to:

d = sqrt(xdistance^2 + ydistance^2)

Where:

xdistance = (x2 - x1)

and

ydistance = (y2-y1)

Edited by superman3275

##### Share on other sites

Thanks to everyone for the advice. I agree with pretty much everything said.

Only if your profiler actually tells you that this particular multiply is killing your performance should you actually change it.

Programmers (even ones who have been doing it for years) are horrible at knowing what is slowing down their code. Use and love your profiler

I'm always afraid to rely so heavily on profiling. A game (or any complex program) seems like a huge mess of fluctuating circumstances, where any small dynamic change can have a noticeable effect on performance. Is there a way to find the bottlenecks when the necks can morph and move around as the game state changes? In addition, newer programmers may write a lot of sluggish routines, which when used together, wouldn't create a bottleneck at all, right? Or at least not one that stands out much.

I don't think these two things are much of an issue for experienced programmers that know the impact that each code fragment they write will have on the CPU/GPU, but its especially challenging for those who are mostly in the dark about such things.

##### Share on other sites

Thanks to everyone for the advice. I agree with pretty much everything said.

Only if your profiler actually tells you that this particular multiply is killing your performance should you actually change it.

Programmers (even ones who have been doing it for years) are horrible at knowing what is slowing down their code. Use and love your profiler

I'm always afraid to rely so heavily on profiling. A game (or any complex program) seems like a huge mess of fluctuating circumstances, where any small dynamic change can have a noticeable effect on performance. Is there a way to find the bottlenecks when the necks can morph and move around as the game state changes? In addition, newer programmers may write a lot of sluggish routines, which when used together, wouldn't create a bottleneck at all, right? Or at least not one that stands out much.

I don't think these two things are much of an issue for experienced programmers that know the impact that each code fragment they write will have on the CPU/GPU, but its especially challenging for those who are mostly in the dark about such things.

Yes. You find these bottlenecks through Profilers.

• 11
• 11
• 9
• 12
• 10