The first code sample computes:
M_PI * ((r * r) - (d/2) * (d/2));
The second computes:
(M_PI * (r * r)) - (d/2) * (d/2);
A call to pow(n, 2)
is the same as n * n
, on most compilers. The exact same assembly will be emitted. This is due to an optimization called "strength reduction" -- most pow()
implementations will check to see if the exponent is 2, and reduce that case to a single multiplication. The unoptimized version is slightly more expensive since it requires a function call and some branching.
Note that M_PI
is not part of the C standard, so you can use the equivalent, which compiles to the exact same code:
double M_PI = 4.0 * atan(1.0);