0
0
0nk0l0g2016-02-25 23:17:00
Programming
0nk0l0g, 2016-02-25 23:17:00

Why is multiplication faster than division?

I heard that the multiplication operation is faster than the division operation, so it's better to multiply by 0.5 than divide by 2.
I want to know if this is true for all programming languages ​​and why this happens.
Thank you.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Dimonchik, 2016-02-26
@0nk0l0g

about "it's better to multiply by 0.5" xs, because it's a floating point, but, probably, yes,
and everything is simple: a
modern computer, and a computer 20 years ago, and a computer 30 years ago - this is an ordinary transistor
(when a photon percent is invented, it will be not ordinary, though ..)
but an ordinary transistor has only two states: 0 (no voltage) and 1 (current flows)
and the transistor can only do two actions with these two states: the
first: addition (0 + 1 \u003d 1, 0 + 0 \u003d 0, 1+1 \u003d 01, etc.)
second: sign change (honestly - I don’t remember where the current goes in this case)
that’s all. The computer can't do anything else.
therefore, arithmetic is implemented something like this:
addition: the first number is COMPLICATED the second number
subtraction: the first number is CHANGED SIGN the second number is COMPLICATED the first number
is multiplied: many times COMPLICATED and pushed onto the stack, which is also COMPLICATED
division: many times COMPLETE and pushing onto the stack + a little less times CHANGING SIGN and again onto the stack
As you can see, when dividing the most operations, by in essence, division is a bunch of additions with a different sign, and the size of the heap is greater than a bunch of multiplications
, hence the legs grow at the accuracy of the floating point - COMPLEX and CHANGE SIGN also need to be done a bunch of times, depending on the accuracy

S
sitev_ru, 2016-02-26
@sitev_ru

It's not necessary at all ... Here I have achieved that multiplication is slower than division:
rextester.com/NMZ47337

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    cout << "Test mul" << endl;
    int tick1 = GetTickCount();
    
    double a = 1000000000000;
    for (int i = 0; i < 1000000; i++) {
        a *= 0.99;
    }
    
    int tick2 = GetTickCount();
    
    cout << "time = " << tick2 - tick1 << endl;
    cout << a << endl;

    cout << "Test div" << endl;
    tick1 = GetTickCount();
    
    a = 1000000000000;
    for (int i = 0; i < 1000000; i++) {
        a /= 1.0001;
    }
    
    tick2 = GetTickCount();
    
    cout << "time = " << tick2 - tick1 << endl;
    cout << a << endl;
}

These are the numbers I got:
Test mul
time = 47
2.42092e-322
Test div
time = 16
3.73872e-32

Three times slowdown)

M
Mikhail Usotsky, 2016-03-03
@Aquarius-Michael

Because division will require a residual result in an intermediate division step. Therefore, the operation will always be sequential. For addition, subtraction and multiplication, you can use acceleration schemes due to the parallelization of the calculation. With division, such a number will not work, or you will get a cumbersome circuit with a very long delay interval, which makes it easier to do this sequentially. Therefore, in programming, it is preferable to avoid division operations as much as possible.
PS You can learn about this from books on circuitry. There is a section for the analysis of material with the representation of the processing of numbers in a computer.

U
uvelichitel, 2016-02-25
@uvelichitel

Not for all compilers, not in all circumstances, but YES multiplication is cheaper than division. Just try multiplying and dividing on paper in a column and see why immediately.

T
Timur Sergeevich, 2016-02-25
@MyAlesya

It does not depend on the PL, but on the compiler + processor on which these operations occur.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question