What code need more cpu instructions?
EXAMPLE A
EXAMPLE BPHP Code:
....
#define AZ 20
....
x -= AZ
....
are they the same?PHP Code:
....
#define AZ -20
....
x += AZ
....
and if AZ would be a variable and not a defined constant?
Printable View
What code need more cpu instructions?
EXAMPLE A
EXAMPLE BPHP Code:
....
#define AZ 20
....
x -= AZ
....
are they the same?PHP Code:
....
#define AZ -20
....
x += AZ
....
and if AZ would be a variable and not a defined constant?
In short - yes. Why do you ask?
Depending on the arch, but it may even be the same instruction.
On all processors that I know of, subtract and add take the same amount of clock-cycles, and the compiler may well realize that the two alternatives you have given are the same anyways as subtracting a number _IS_ the same as adding the negative of the number - so the compiler may well produce code to subtract the positive, or add the negative for either of your bits of code - if one is "better" for some reason [better here means shorter or faster].
Sometimes a large negative number will occupy more space because the processor doesn't support small negative numbers, so adding a negative number may increase the code-size, but x86 has a 8-bit signed value, so for 20, it is the same length for both subtract and add.
--
Mats