Why is a mod (%
) operation more expensive than a 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.
}
+
? ^^ – Chaetognatha
. If compiled with optimization, neither loop does any work. If without optimization, it's not a very meaningful benchmark. – Hectograph