Блог пользователя tawhidmonowar

Автор tawhidmonowar, история, 14 месяцев назад, По-английски

In programming, “++i” and “i++” are both used to increment the value of a variable by 1, but the difference is in the order in which the increment operation and the use of the variable occur.

When it comes to the difference between “++i” and “i++”, it’s important to understand how each operator works. “++i” is known as the pre-increment operator, which increments the value of ‘i’ immediately and returns the incremented value. On the other hand, “i++” is known as the post-increment operator, which increments the value of ‘i’ but returns the original value that ‘i’ held before being incremented.

Example:

Code

In terms of performance, “++i” is sometimes faster than “i++” and is never slower than "i++". For intrinsic types like int, it doesn’t matter: “++i” and “i++” are the same speed. However, for class types like iterators, “++i” very well might be faster than “i++” since the latter might make a copy of the this object.

In conclusion, while there may be some performance differences between “++i” and “i++”, it’s generally recommended to use “++i” unless you specifically want the postfix semantics.

I hope this information will be helpful to you.

  • Проголосовать: нравится
  • +36
  • Проголосовать: не нравится

»
14 месяцев назад, # |
  Проголосовать: нравится +22 Проголосовать: не нравится

Based

»
14 месяцев назад, # |
Rev. 2   Проголосовать: нравится +10 Проголосовать: не нравится

Remember that, most of the time compiler is smarter than you think.

  • »
    »
    14 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Absolutely! It's important to acknowledge that compilers are incredibly intelligent and often optimize our code in ways we might not anticipate. Understanding how compilers work can help us write more efficient and effective code. Thank you for highlighting this valuable point!

    • »
      »
      »
      12 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Understanding how compilers work

      I suppose it would be nicer to have some Assembly code (or perhaps some C++ code with similar steps).

»
14 месяцев назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

In many cases, the compiler will optimize i++ to ++i, so it's not necessary to use ++i everywhere in code. I personally just use post-increment because that's how I like my code to look.

»
12 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

After backing up the code I had a problem with it, reading this blog cleared it up tnx bhai ^_^

»
12 месяцев назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

If using this to speed up your code by 1 nanoseconds matter so much I recommend just finding a better approach

»
12 месяцев назад, # |
  Проголосовать: нравится -11 Проголосовать: не нравится

++i works faster I benchmarked it in various cores on various c++ versions on various compiler versions and formed a cayley table and published a paper which got ++1000 citations from recently colonized mars

»
12 месяцев назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Good entry but I will try to more elaborate on "why"

Foo& Foo::operator++()   // called for ++i
{
    this->data += 1;
    return *this;
}

Foo Foo::operator++(int ignored_dummy_value)   // called for i++
{
    Foo tmp(*this);   // variable "tmp" cannot be optimized away by the compiler
    ++(*this);
    return tmp;
}  

i++ is usually slower due to the fact that it creates a new temporary object for its operation whereas ++i works directly on the object