First case:
void lowTermReduction(int numerator, int denominator) {
int gcd = GCD(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
Second case:
void lowTermReduction(int numerator, int denominator) {
int gcd = GCD(numerator, denominator);
if (gcd != 1) {
numerator /= gcd;
denominator /= gcd;
}
}
Which one is more efficient (fast)?
In the first case, the function always performs the division, also if this division is by 1 and doesn't change the values in numerator
and denominator
. I don't know if the CPU is faster in doing 12/1 or 12/2, but I think that is quite the same.
In the second case, instead, the function does the division only when the Greater Common Divisor is different by 1. But in this case it performs a logical operation if
.
There are efficiency differences? And if so, are them relevan?