Here's a silly fun question:
Let's say we have to perform a simple operation where we need half of the value of a variable. There are typically two ways of doing this:
y = x / 2.0;
// or...
y = x * 0.5;
Assuming we're using the standard operators provided with the language, which one has better performance?
I'm guessing multiplication is typically better so I try to stick to that when I code, but I would like to confirm this.
Although personally I'm interested in the answer for Python 2.4-2.5, feel free to also post an answer for other languages! And if you'd like, feel free to post other fancier ways (like using bitwise shift operators) as well.
Java android, profiled on Samsung GT-S5830
Results?
Division is about 20% faster than multiplication (!)
If you want to optimize your code but still be clear, try this:
The compiler should be able to do the divide at compile-time, so you get a multiply at run-time. I would expect the precision to be the same as in the
y = x / 2.0
case.Where this may matter a LOT is in embedded processors where floating-point emulation is required to compute floating-point arithmetic.
Technically there is no such thing as division, there is just multiplication by inverse elements. For example You never divide by 2, you in fact multiply by 0.5.
'Division' - let's kid ourselves that it exists for a second - is always harder that multiplication because to 'divide'
x
byy
one first needs to compute the valuey^{-1}
such thaty*y^{-1} = 1
and then do the multiplicationx*y^{-1}
. If you already knowy^{-1}
then not calculating it fromy
must be an optimization.As with posts #24 (multiplication is faster) and #30 - but sometimes they are both just as easy to understand:
~ I find them both just as easy to read, and have to repeat them billions of times. So it is useful to know that multiplication is usually faster.
Python:
multiplication is 33% faster
Lua:
=> no real difference
LuaJIT:
=>it's only 5% faster
conclusions: in Python it's faster to multiply than to divide, but as you get closer to the CPU using more advanced VMs or JITs, the advantage disappears. It's quite possible that a future Python VM would make it irrelevant
Just going to add something for the "other languages" option.
C: Since this is just an academic exercise that really makes no difference, I thought I would contribute something different.
I compiled to assembly with no optimizations and looked at the result.
The code:
compiled with
gcc tdiv.c -O1 -o tdiv.s -S
the division by 2:
and the multiplication by 0.5:
However, when I changed those
int
s todouble
s (which is what python would probably do), I got this:division:
multiplication:
I haven't benchmarked any of this code, but just by examining the code you can see that using integers, division by 2 is shorter than multiplication by 2. Using doubles, multiplication is shorter because the compiler uses the processor's floating point opcodes, which probably run faster (but actually I don't know) than not using them for the same operation. So ultimately this answer has shown that the performance of multiplaction by 0.5 vs. division by 2 depends on the implementation of the language and the platform it runs on. Ultimately the difference is negligible and is something you should virtually never ever worry about, except in terms of readability.
As a side note, you can see that in my program
main()
returnsa + b
. When I take the volatile keyword away, you'll never guess what the assembly looks like (excluding the program setup):it did both the division, multiplication, AND addition in a single instruction! Clearly you don't have to worry about this if the optimizer is any kind of respectable.
Sorry for the overly long answer.