Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why division operation typically uses more resources in computer?

Tags:

c

math

I am reading a C book. In the Arithmetic Expression section, they say:

"Division typically uses more resources.  To avoid division, we multiply rather than divide. For example, we multiply by 0.5 rather than divide by 2.0."

Why Division typically uses more resources?. Can anyone give me a detail explanation, please?

Thanks a lot.

like image 365
ipkiss Avatar asked Feb 24 '11 23:02

ipkiss


1 Answers

Binary Multiplication is simple using the peasant algorithm - you basically shift, then sum: http://en.wikipedia.org/wiki/Multiplication_algorithm#Peasant_or_binary_multiplication

Binary Division is much harder, as it's a sequence of subtractions (like long division you may have done in school). The main algorithm class is called 'radix', which you can see an example of here: http://www.bearcave.com/software/divide.htm

Remember though - measure first, then optimise. It's much easier to maintain code that matches the problem domain than code that is already optimised.

like image 176
Josh Avatar answered Oct 25 '22 09:10

Josh