Thread: + and -

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    + and -

    What code need more cpu instructions?

    EXAMPLE A
    PHP Code:
    ....
    #define AZ     20
    ....
    -= AZ
    .... 
    EXAMPLE B
    PHP Code:
    ....
    #define AZ    -20
    ....
    += AZ
    .... 
    are they the same?

    and if AZ would be a variable and not a defined constant?
    Last edited by Kempelen; 11-07-2008 at 04:03 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    In short - yes. Why do you ask?

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Depending on the arch, but it may even be the same instruction.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed