if ( n == 1 ) return x;
if (n % 2 == 1) return pow(x,n/2,n/2) * pow(x,n/2,n/2) * x;
return pow(x,n/2,n/2) * pow(x,n/2,n/2);
}
will compiler optimzie above code to this one ?
int pow (int x, int n){
if ( n == 1 ) return x;
int temp = pow(x,n/2);
if ( n%2 == 1) return x * temp * temp;
return temp * temp ;
}
Mostly because you are calling a function and it doesn’t know if it will alter a state
Обсуждают сегодня