Fastest way to calculate a/b+c/d with float

Asked

Viewed 78 times

1

You can operate four floats in the formula a/b+c/d (two divisions and a sum) mathematically equivalent to the formula (a*d+b*c)/(b*d) (three products, one division and one sum).

It is known that more instructions increase the number of rounding (tending to increase errors) but that processor instructions for dividing (between floats yes, but mostly between two integers) it costs a lot more in performance than multiplication.

Still, it’s hard to decide between the two modes in terms of performance. What’s faster? A split or three products? Which of the two formulas in the most diverse languages tends to result in better performance?

1 answer

3


It’s hard and it’s easy.

Universally it’s difficult because every architecture, including its versions, can make a lot of difference. Not only that, the compiler you are using and linked options can make a difference, including that we may be talking about different languages.

Specifically it is easy, if you want to know on a specific machine which is faster just test the two on it and see which one runs faster, there is no secret. Or almost doesn’t, because you need to know that execution can vary on some architectures.

In fact the division costs expensive, but 3 multiplications does not usually cost so cheap, triple can take longer than a split.

There are cases that may not make a difference, language compilers who value performance tend to decompose the expression and try to remodel it in a way that is faster, so if you divide it and the compiler can prove that you exchange itIt gets faster without changing the result and it will do it for you. In languages that do not care about performance this does not occur because it is not her philosophy. If the compiler does not see if it can use another.

The compiler may even use vector statements in some cases if it understands that it is possible and helps something.

If I have to guess I think that the multiplications in the place of the division can give some gain, if the compiler does not do this optimization for you because it gives in the same. If you’re not using C, C++ or something like that, I doubt it’ll make much difference. This type of optimization is the last that should do, there are others that give better results, in some cases several orders of magnitude of difference. And some languages don’t even give much chance of improvement.

But there are other factors that can influence the general execution, and even the specific one, after all have processors that the same instruction can have a different cost depending on the case of execution, ie in each execution can give a slightly different cost. It is very complicated to guarantee the time. You can establish a track.

Just remembering that you should only worry about it if it will make a lot of difference, is something that helps too much, once measured, and you should know that the situation can change.

Today access to memory, and mainly allocation, is much more important to gain performance.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.