CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 24

Hybrid View

  1. #1
    Join Date
    Sep 2002
    Posts
    71

    Re: C++ language: Why should I use ++i instead of i++ ?

    [Andreas]: The following question is asked in regard to the following FAQ:


    You mention that for regular variables (like int) there is no performance gain. Is this statement true for C++ but not for C? I've come across several people who do ++i all the time rather than i++ (in C) claiming efficiency gains.
    Last edited by Andreas Masur; July 29th, 2005 at 12:42 PM.

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Why should I use '++i' instead of 'i++'?

    [ Split thread ]

  3. #3
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647

    Re: C++ language: Why should I use ++i instead of i++ ?

    Quote Originally Posted by C++ Newbie
    [Andreas]: The following question is asked in regard to the following FAQ:


    You mention that for regular variables (like int) there is no performance gain. Is this statement true for C++ but not for C? I've come across several people who do ++i all the time rather than i++ (in C) claiming efficiency gains.
    normally it's a question of whether your compiler is able to "take a look" inside operator's implementation and optimise away a temporary variable if it's really not needed (not used by the caller code). for native compiler types it's possible and, more to say, with a clever compiler it's even possible with a user defined type under some circumstances.
    e.g. when class's operator++ (int) is defined with "inline" specificator.

    the recent c compilers do this optimization (at least, I have checked it out with gcc 3.3.4). what's about using ++i instead of i++? I guess it becomes a matter of habbit with time. e.g. if I know that I donot need an old value of the variable, I just don't use i++ even for for-loops.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: C++ language: Why should I use ++i instead of i++ ?

    Personally, I think ++i reads better: "increment i", rather than i++ - "i increment".
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: C++ language: Why should I use ++i instead of i++ ?

    Quote Originally Posted by C++ Newbie
    [Andreas]: The following question is asked in regard to the following FAQ:


    You mention that for regular variables (like int) there is no performance gain. Is this statement true for C++ but not for C? I've come across several people who do ++i all the time rather than i++ (in C) claiming efficiency gains.
    Since C does not allow to overload operator ++, you can only use this operator on builtins types, so the performances of prefix and postfix versions are basically identical (at least if the return value is not used, the assembly code generated will be the same).

    But, it is better to use ++i
    • It is a good habit to use ++i, so you don't have to think about what version you use, and you will not accidentally use i++ where you should have used ++i
    • It is easier for the programmer's mind, to don't matter which operator to use.
    • If a day, you want to replace your C-style pointers to array by iterators of a STL container, you will need less code modification for optimal performances.
      Note that for an easy conversion, it is generally good to typedef pointers that you use much, so you can replace them by iterators by changing a single line of code.
    • As Graham said, ++i can be literally read "increment i" which is maybe more intuitive than i++. But for an advanced programmer i don't think it makes any difference.


    I don't understand why books i read used i++ everywhere, so, during a long time, i had the bad habit to use i++.
    But i think that it is far better to get the good habits (especially if they are easy to take) from the begining of C++ learning.

  6. #6
    Join Date
    Jun 2002
    Posts
    1,417

    Re: C++ language: Why should I use ++i instead of i++ ?

    I also use i++ because of habbit over the past 20 or so years and never use ++i. Also use it on c++ iterators in for loops and for most other pointers. None of my programs are time-critical so speed difference of a couple clock ticks is unimportant.

  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: Why should I use '++i' instead of 'i++'?

    ++i as know as PreIncrement
    and i++ as post increment

    if you consider then in a loop
    like for or something else
    there is no difference between ++ior i++

    but as we know ++i first increment the value by one then print

    but i++ first print the value then increment.

    means i++ uses more cpu cycle.

    so it's upto you which one you want to use .most people like to use i++.becuase from the start onwards they think or they start write like this only i++ means increment the value of i by 1.
    they think ++i at the time when they have to use preincrement operator

    so better to use ++i.to minimize your cpu cycle.and improve your speed

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Why should I use '++i' instead of 'i++'?

    Quote Originally Posted by humptydumpty
    but as we know ++i first increment the value by one then print

    but i++ first print the value then increment.
    Print? Print what? Did you mean evaluate?

    Quote Originally Posted by Graham
    Personally, I think ++i reads better: "increment i", rather than i++ - "i increment".
    Personally, I think the opposite.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Jun 2002
    Posts
    1,417

    Re: Why should I use '++i' instead of 'i++'?

    Quote Originally Posted by humptydumpty
    so better to use ++i.to minimize your cpu cycle.and improve your speed
    I might agree with that in an application that does that millions of times a second -- but in other applications it doesn't make a hoot which you use. Whether you use ++i or i++ is merly a personal decision, beyond that it is a moot question. Who the he11 cares whether i++ takes 1 or 2 clock ticks more than ++i??? Your time and efforts to optimize a program are much better spent elsewhere. [edit] you might care if you are compiling for a very sloooooooooow computer[/edit]
    Last edited by stober; July 30th, 2005 at 07:39 AM.

  10. #10
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Why should I use '++i' instead of 'i++'?

    Quote Originally Posted by stober
    I might agree with that in an application that does that millions of times a second -- but in other applications it doesn't make a hoot which you use. Whether you use ++i or i++ is merly a personal decision, beyond that it is a moot question. Who the he11 cares whether i++ takes 1 or 2 clock ticks more than ++i??? Your time and efforts to optimize a program are much better spent elsewhere. [edit] you might care if you are compiling for a very sloooooooooow computer[/edit]
    I don't agree.

    • It is better that all programmers use the same convention.
      So why not use preincrement, since it is a little better?
    • Generally preincrement and postincrement have the same performance, but for iterators as deque::iterator, the difference not negligible.
      For example, i found that for a copy from deque to deque, using postincrement is more than two times slower than preincrement, with my computer, and my STL.
      For complex algorithms this may be not an issue, if much processing is done between to calls to the increment iterator.

      But, since STL is everywhere nowadays, it is probable that for some actual programs, and some future programs, the CPU time executing various increment operators on various iterators of various containers, may use 50% of CPU cycles.
      In fact when using deque, generally dereferencing is fact, but incrementation is slow, so the incrementation may take 75% CPU cycles on some algorithms using deque on a simple data structure (deque<char> for example).

      If you replace only one postincrement by one preincrement in your whole code, you will probably not notify the difference, but if you replace them all, it can make a big difference.
    • Even if the two things are nearly identical, why not using the best one?
      Don't say that it is easier to write "i++" than "++i", for the programmer learning C++, there is no difference choosing one or the other, so it is better to get the good habit from the start.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured