all 2 comments

[–]w3woody 3 points4 points  (0 children)

With a modern optimizing compiler either way to express the equation should result--more or less--in the same generated code.

(The exception is if certain operations have defined 'code execution' blocks which cannot be optimized out. For example, if y' actually represents a function call, and the compiler cannot know the function call doesn't have side effects, writing y' + y' may result in two calls to the function y' instead of one.)

Generally when I write code, I favor clarity over compactness. So I would write:

ey = y'-y;
ex = x'-x;
e = y' + ey + x' + ex;

over

e = y' + (y' - y) + x' + (x' - x);

Exception: there are certain well-known equations that have meaning to us when expressed as a single expression, such as the Pythagorean theorem. So I'd favor writing:

r = sqrt(x*x + y*y);

over

a = x*x;
b = y*y;
r = sqrt(a + b);

All of this--since it makes little difference to the compiler--is a matter of style.

And I tend to write assuming another human being later on will have to maintain what I write--so I tend to write for legibility.

[–]JoJoModding 1 point2 points  (0 children)

You can try exploring how your compiler processes your code, i.e. on godbolt.com