Why is MOD
operation more expensive than multiplication
by a bit more than a factor of 2
? Please be more specific about how CPU performs division operation and returns the result for MOD operation.
In the following example the threads each run for a second. The test was performed on a SPARC
processor.
// multiplication
void someThread() {
int a = 10234;
while (true) {
opers++;
a = a * a;
a++;
}
// opers ~ 26 * 10^6 in a sec.
}
// MOD
void someThread() {
int a = 10234;
while (true) {
opers++;
a = a % 10000007;
a++;
}
// opers ~ 12 * 10^6 in a sec.
}
MOD is a division operation, not a multiplication operation. Division is more expensive than multiplication.
More information about the MOD operation here: http://en.wikipedia.org/wiki/Modulo_operation