59

Fun/Interesting fact:
"++i" can be slightly faster than "i++"

Because  "i++" can require a local copy of the value of "i" before it gets incremented, while "++i" never does. In some cases, some compilers will optimize it away if possible... but it's not always possible, and not all compilers do this.

Comments
  • 15
    Hmmm, c++ porn,
    I indeed knew about it but have a ++ for spreading the word :)
  • 0
    Thanks :)
    but this does not only count for c++
  • 8
    i = 5
    x = i++
    print(x)
    >> 5

    i = 5
    x = ++i
    print(x)
    >> 6

    So it depends on how you want to increment the number.
  • 1
    @Deserter Yes, because one takes effect already while the other only after the end of the iteration loop/function/scope where the increment is.
    But what the OP is talking about is more applicable in cases of for loops, where both yield the same results but if the compiler does not optimize it using ++i is marginally faster.
  • 2
    Does this work in JavaScript?
  • 1
    @Deserter yea ofcouse there is a use case when you want i++ but I meant when you can use both, use ++i because it can be slightly faster :)
  • 2
    @karthik2502 yes this also applies to javascript.

    Also welcome to devrant :D
  • 3
    I have been here for quite a bit, I only read the rants though! Thanks :)
  • 0
    @karthik2502 I even thought when i pressed 'Post': 'I should have checked his profile first' hahaha.

    But your welcome :)
  • 4
    Python guys are losing shit !
  • 0
    @SISheogorath i am going to fuck things up if I start doing that haha

    Also my coworkers are going to hate me 😂
  • 2
    ++i does work on JavaScript but not on mobile, i've been stuck for 1 hour this week because of this.
  • 0
    @640509-040147 oh that's annoying!

    Also welcome to devrant to you to!
  • 0
  • 1
    @Deserter I think you mean print(++i) and print(i++) because in the code you gave I is always 6.
Add Comment