Operation of addition and multiplication

I know that the addition operation is more trivial than the multiplication operation. But will there be a difference in the execution time of 123456 * 3 and 123456 + 123456 + 123456 ?

How exactly does multiplication work?

Are there multiplication algorithms in different programming languages?

How does multiplication look at low-level (for example, assembler code)?

+1
source share
2 answers

In x86 assembly language, the addition and multiplication operations are as follows:

ADD [operand1], [operand2] where operand 1 can be a register, operand 2 can be a register, constant or memory address Requires 1 to 7 hours depending on the processor model and type of operand2

MUL [operand]; for unsigned multiplication, multiplies the contents of the battery register (AL, AX, EAX) by the operand, which can be a register or a memory address. And again, depending on the type of operand and processor model, it takes 12-38 hours. There is also a MUL version that has signed multiplication.

This is the main assembly language without modern SIMD extensions such as SSE, etc. Actual speed, as mentioned above, depends on compiler optimization.

A smart compiler will most likely replace your 123456 + 123456 + 123456 with 3 * 123456

+2
source

Premature optimization is the root of all evil :)

What you give to the compiler is not what you return after the optimization step, therefore, while the theoretical addition is faster, in real conditions you can never be sure what the result will be (not to mention when you take into account SSE or other processor instructions that the compiler can use).

+1
source

All Articles