C / C ++: float comparison speed

I check that the float is not zero. The float cannot be negative. So is it faster to do float != 0.0f or this float > 0.0f ?

Thanks.

Edit: Yes, I know this is micro-optimization. But this will be called up every time through my game loop, and I would still like to know.

+4
source share
5 answers

Consider for entertainment purposes only:

Only 2 floating point values ​​are compared with 0f : zero and negative zero, and they differ only 1 bit. Thus, a circuit / software emulation that checks if 31 non-sign bits are transparent will do so.

Comparison >0f bit more complicated, since negative numbers and 0 lead to false numbers, positive numbers lead to true ones, but NaNs (of both signs) also lead to false numbers, so it’s a bit more than just checking the sign bit.

Depending on the floating point mode, any operation may result in a super-accurate result in the floating point register being rounded up to 32 bits before comparison, so the estimate is even there.

If there was a difference at all, I would expect that != Would be faster, but I would not expect that there would be a difference, and I would not be very surprised if I am mistaken in some implementation.

I assume that your proof that the value cannot be negative is not subject to floating point errors. For example, the calculations in lines 1/2.0 - 1/3.0 - 1/6.0 or 0.4 - 0.2 - 0.2 can lead to positive or negative values ​​if errors accumulate and are not canceled, so, apparently, nothing like this happens. On the actual use of the floating point test for equality with 0, you need to check whether you assigned it a literal 0 . Or the result of some other calculation is guaranteed to have a result of 0 in a float , but it can be tricky.

+5
source

There will probably not be a noticeable difference in performance.

+8
source

It is impossible to give a clear answer without knowing your platform and compiler. Standard C does not specify how floats are performed.

On some platforms, yes, on other platforms, no.

If in doubt, measure.

+3
source

As far as I know, f != 0.0f will sometimes return true if you think it should be false.

To check if a floating-point number is non-zero, you should do Math.abs(f) > EPSILON , where EPSILON is a mistake you can tolerate.

Performance should not be a big problem in this comparison.

+2
source

This is almost certainly micro-optimization that you should not do until you get quantitative data showing that this is a problem. If you can prove that this is a problem, you should figure out how to make your compiler displayed by the machine instructions that it generates, then take this information and go to the data book for the processor used and see the number of clock cycles required for alternative implementations of the same logic. Then you must measure again to make sure you see the benefits, if any.

If you do not have data showing that the performance problem is related to the implementation, which most clearly and simply represents the logic of what you are trying to do.

0
source

Source: https://habr.com/ru/post/1335635/


All Articles