20

Everyday I change all of my colleague's i++ in ++i in for loops, and he changes my ++i in i++. This goes on forever...

Comments
  • 2
    Does he know that writing ++i considered good practice? 🤔🤔
  • 4
    Don't compilers optimize i++ anyway? Funny, I never checked this.
  • 2
    ...and this is why we use loops. If you had described this process recursively, you may have described a process that might have caused a stack overflow in a computer with memory of discrete length. If we asume humans are computers with limited memory capacity, you would have described a process that might have killed someone... eventually. Oh shit I just realized, this would also imply a recursive function alternating between two processors with shared memory... I smell a race condition! But to @rememberMe ‘s point, compiler optimization might make this whole problem go away. In this case a compiler could optimize via tail call optimization rendering the recursive function an iterative process. God I fucking love compilers!
  • 3
    Right, I was right, compilers do optimize if i is some plain old data type like int.

    But if i is some fancy class object, then the compiler may not be as to optimize between pre and post increment, because post generally involves a temp object.

    However I distinctly recall reading somewhere that since preincrement introduces a data dependency, it's not so good if you're on a deeply pipelined CPU (could stall). I could be wrong though.

    @FrodoSwaggins what do you think?
  • 1
    ++i is faster because the program doesn't need to keep a copy (I feel like most compilers would optimize it out though)
  • 0
    @RememberMe even if perf isn't a problem, IMHO a good dev always makes sure to optimize for that
  • 4
    Wait, isn't there a difference between i++ and ++i? Fure if it's in something like for(int i=0; i<5; ++i) { ...} It doesn't matter, as all that happens is, that i is incremented by one... However

    int i = 5;
    int j = i++;
    assert(i == 6 && j == 5)

    i = 5;
    int k = ++i;
    assert(i == 6 && k == 6)

    AFAIK, for a j = i++; a computer will do j = i; i = i+1; while an j = ++i; results in i = i+1; j = i;

    So in general cases you can't treat them as equal, but the amount of work a computer has to do (on assembly level) should be the same. Load value to register, icrement by one, store value (possibly store value to other var, before or after incrementing).

    If you do really care about this kind of optimizations, you should decrement your loops, like for(int i=5; i; i--) { ... } As there should be a register jolding the falue 0 and thus only an equal comparission between i and 0 has to be done, whereas with i < 5 both i and 5 have to be loaded in a register and a lt comparission has to be done.
  • 3
    @gnulinuxer4fun I would go for code readability over micro-optimizations, though admittedly it's arguable here that ++i and i++ are equally readable so it doesn't matter much. And even a half-decent compiler would optimize it away anyway. And besides, it's really hard to tell in these cases because of the vast complexity of the hardware system (such as the preincrement leading to a possibe stall in the pipeline for a complex object), so you don't really know till you profile anyway. In that case I would generally prefer to not make assumptions and write in the most "obvious" manner possible (following good conventions and practices though) and then profile to see what's really happening.

    @Wack we're talking about the increment in a for loop only. Since it makes no difference to the meaning of a program, they should ideally be treated the same way.
  • 0
    @RememberMe in a good design, micro optimizations go well together with readability
  • 0
    @FrodoSwaggins right, makes sense, thanks.
  • 2
    I'd like to answer to everybody. We are both aware that ++i and i++ are perfectly equivalent in for loops, that's why we keep changing them, it's just a matter of preference, like the open curly bracket on new line or at the end of line. And of course we are talking about integer i.

    But obviously they mean two different things, especially with assignments. Compilers today are "smart" enough to optimize the code so they are perfectly interchangeable when they just mean "increment integer i by 1". If i is some kind of complex class and ++ is overloaded to make operations beyond the simple increment by one, then of course ++i and i++ are very different and the compiler may not be able to optimize.

    Our "fight" is based just on taste. I'm coming from a low level C++ and assembly background and I tend to overthink a lot of small things believing that I'm in charge of optimization and the compiler has to do the least amount of work possible. So i prefer ++i for simple increment
Add Comment