Reputation: 1053
Is 3*3
faster, or takes the same number CPU cycles as 1000*1000
(values are C int
). Is this claim applied to all other arithmetic operators, including floating point ones?
Upvotes: 0
Views: 78
Reputation: 111279
CPUs typically implement multiplication for fixed sized numbers in hardware, and no matter which two numbers you pass in, the circuitry is going to run through all the bits even if most of them are zeros. For examples of how you can multiply two numbers in hardware see https://en.m.wikipedia.org/wiki/Binary_multiplier
This means the time it takes to multiply two "int"s in C is pretty much a constant.
Caveat: You may find that multiplication by a power of 2 is much faster than multiplication in general. The compiler is free to not use the multiply instruction and replace it with bit shifts and adds if it produces the correct result.
Upvotes: 2