Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use multiplication or division?

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.

like image 394
edmundito Avatar asked Oct 22 '08 16:10

edmundito


People also ask

Is multiplication more efficient than division?

Multiplication is faster than division. At university I was taught that division takes six times that of multiplication. The actual timings are architecture dependent but in general multiplication will never be slower or even as slow as division.

Which is harder multiplication or division?

Division is typically the hardest math concept for kids to learn. A division equation is made up of three parts: The dividend is the number that's being divided (the first number in the problem).


2 Answers

Python:

time python -c 'for i in xrange(int(1e8)): t=12341234234.234 / 2.0' real    0m26.676s user    0m25.154s sys     0m0.076s  time python -c 'for i in xrange(int(1e8)): t=12341234234.234 * 0.5' real    0m17.932s user    0m16.481s sys     0m0.048s 

multiplication is 33% faster

Lua:

time lua -e 'for i=1,1e8 do t=12341234234.234 / 2.0 end' real    0m7.956s user    0m7.332s sys     0m0.032s  time lua -e 'for i=1,1e8 do t=12341234234.234 * 0.5 end' real    0m7.997s user    0m7.516s sys     0m0.036s 

=> no real difference

LuaJIT:

time luajit -O -e 'for i=1,1e8 do t=12341234234.234 / 2.0 end' real    0m1.921s user    0m1.668s sys     0m0.004s  time luajit -O -e 'for i=1,1e8 do t=12341234234.234 * 0.5 end' real    0m1.843s user    0m1.676s sys     0m0.000s 

=>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

like image 77
Javier Avatar answered Sep 28 '22 02:09

Javier


Always use whatever is the clearest. Anything else you do is trying to outsmart the compiler. If the compiler is at all intelligent, it will do the best to optimize the result, but nothing can make the next guy not hate you for your crappy bitshifting solution (I love bit manipulation by the way, it's fun. But fun != readable)

Premature optimization is the root of all evil. Always remember the three rules of optimization!

  1. Don't optimize.
  2. If you are an expert, see rule #1
  3. If you are an expert and can justify the need, then use the following procedure:

    • Code it unoptimized
    • determine how fast is "Fast enough"--Note which user requirement/story requires that metric.
    • Write a speed test
    • Test existing code--If it's fast enough, you're done.
    • Recode it optimized
    • Test optimized code. IF it doesn't meet the metric, throw it away and keep the original.
    • If it meets the test, keep the original code in as comments

Also, doing things like removing inner loops when they aren't required or choosing a linked list over an array for an insertion sort are not optimizations, just programming.

like image 21
Bill K Avatar answered Sep 28 '22 01:09

Bill K