21

Is "++i" more efficient or "i++" ?

P.S. I already wasted more cpu cycles by posting this, than I would ever waste by using either of them....

Comments
  • 2
    Afaik it doesn't matter, the only difference is the return value (i.e. the i before or after the addition)
  • 2
    @lucascorpion I am not asking. It's a joke....
  • 5
    @playmast3r Damn you, and damn my tired brain :P
  • 1
    In some cases yes, but I wouldn't bother unless you're really desperate :P
  • 2
    It depends:
    It's your i angry? Put it in the front of ++ so you can see when she misbehaves.

    If you have a cool, relaxed i instead, the order doesn't matter.
  • 0
    Yes it matters but your compiler will optimize it away
  • 0
    loop truu an array and start witth 0++
    i is/are berter than array[i+1] as lonng as you donn¨t code druk
  • 1
    Yes, the answer is yes. ++i is more efficient because when you use the ++ operator in such a way, before the variable, it will up the variable by one and immediately return it.

    x = x + 1;
    return x;

    However, i++ makes a temporary variable, meaning it takes more memory, it assigns the current value of i to the temporary variable and adds one to i and it returns the temporary variable, which is the value before increment.

    int temp = x;
    x = x + 1;
    return temp;

    So, it is a bit less efficient, but you won't die from using i++ over ++i.
Add Comment