My code throughout my project is littered with things that are divided by 2.
I know a general rule of thumb is that multiplication is faster than division, but that probably also is related to the compiler and the platform.
Also, I figure some compilers know that when you are dividing by a constant, it can pre-reciprocate the constant and just multiply. Although in some cases, this could lose precision, right?
So I have a few questions I’m wondering about:
Is 2 a special case for the compiler, since it’s a power of 2?
Does Unity use Mono for Windows builds?
If I have an integer I’m dividing by 2, is it faster to cast it to a float and multiply by 0.5f ?
With Mono, the actual compiling is done at run-time, correct?
With floats, is it more precise to multiply by 0.5f anyway?
That’s not really true on modern CPUs. All basic math operations are pretty much the same speed, and even square roots aren’t vastly slower (in fact the function call overhead for calling Mathf.Sqrt isn’t so far behind the square root operation itself).
Yes.
No, that would be slower; don’t cast unless necessary. The usual way to “speed up” an integer dividing by two would be to bitshift it to the right by one, but in reality it makes no difference. Just keep things simple and divide by two. The compiler might do bitshifting where possible anyway.
Yes and no. The Unityscript/C#/Boo code is compiled to CIL code when you save the script in Unity, and the CIL code is then compiled to machine code at runtime, although some platforms use AOT compilation instead of JIT (such as iOS) and therefore no runtime compiling is done.
No.
The real rule of thumb is to write code as cleanly as you can, and don’t worry about trying to “optimize” these things. Micro-optimization like this is best handled by the compiler, though as I mentioned division doesn’t really make any difference these days anyway.
Division, multiplication and modulo with powers of two are faster than any other division, multiplication or modulo operation because they can be converted to bit sifts by the compiler. As with all compiler optimizations you should check whether or not your individual compiler does actually perform these optimizations or not, so I benchmarked this a while ago. I found that it doesn’t make a difference whether you divide by two or write it as a shift operation, Mono appears to do this automatically.
Converting an integer to float and multiplying with 0.5f is not recommendable because every cast will generally cost performance. Furthermore, on your CPU integer operations are way cheaper than floating point operations.
EDIT: Looks like I’m a little too late, but I think I provided some complementary information to Eric’s post.
In theory a floating point division by two can be done by decreasing the exponent of the number by one. However, I don’t know in how far this will be done automatically by the compiler. In general floating point operations aren’t as easily optimizable by the compiler as integer operations, because f1 * f2 / f3 is not necessarily the same as f1 / f3 * f2, even though mathematically the result should be the same. This is caused by the discretization of floating point numbers.
Therefore, I’m quite certain that a division by 2f will not be converted into a multiplication with .5f, but in theory it can (automatically or not) be optimized by simply decreasing the exponent of the number by one. However, to test if this is the case you should benchmark it.