Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Possible inefficiency in post-increment FOR loop?

Possible inefficiency in post-increment FOR loop?

Scheduled Pinned Locked Moved Design and Architecture
c++graphicstutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xpnctoc
    wrote on last edited by
    #1

    I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:

    std::vector v;
    // vector gets filled here....
    for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
    {
    // iteration code here...
    }

    ...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?

    L L P D 5 Replies Last reply
    0
    • X Xpnctoc

      I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:

      std::vector v;
      // vector gets filled here....
      for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
      {
      // iteration code here...
      }

      ...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      i++;
      /*
      * reults in:
      * load i to save location
      * increment i
      * return saved i
      */

      ++i;
      /*
      * reults in:
      * increment i
      * return i
      */

      In reality most modern compilers will optimise the code so it is of little consequence to the majority of programmers. It only becomes an issue if your program needs to run at sub-atomic speeds.

      Just say 'NO' to evaluated arguments for diadic functions! Ash

      1 Reply Last reply
      0
      • X Xpnctoc

        I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:

        std::vector v;
        // vector gets filled here....
        for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
        {
        // iteration code here...
        }

        ...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Their statement is utter non-sense, as Richard already explained. And furthermore they should concentrate on the termination test of the for loop; v.end() is getting evaluated over and over, not necessarily what is required (depends on how "alive" v is). :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Season's Greetings to all CPians.

        L 1 Reply Last reply
        0
        • L Luc Pattyn

          Their statement is utter non-sense, as Richard already explained. And furthermore they should concentrate on the termination test of the for loop; v.end() is getting evaluated over and over, not necessarily what is required (depends on how "alive" v is). :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Season's Greetings to all CPians.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Yeah, I HATE seeing that in loops. Coders think its efficient because they have less code without thinking that a function call in the conditional part of the loop gets called every itteration.

          "It is a remarkable fact that despite the worldwide expenditure of perhaps US$50 billion since 1990, and the efforts of tens of thousands of scientists worldwide, no human climate signal has yet been detected that is distinct from natural variation." Bob Carter, Research Professor of Geology, James Cook University, Townsville

          1 Reply Last reply
          0
          • X Xpnctoc

            I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:

            std::vector v;
            // vector gets filled here....
            for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
            {
            // iteration code here...
            }

            ...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I was going to say you should take a look at this code disassembled to see if there is any inefficiency, but Richard has answered that already.

            "It is a remarkable fact that despite the worldwide expenditure of perhaps US$50 billion since 1990, and the efforts of tens of thousands of scientists worldwide, no human climate signal has yet been detected that is distinct from natural variation." Bob Carter, Research Professor of Geology, James Cook University, Townsville

            1 Reply Last reply
            0
            • X Xpnctoc

              I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:

              std::vector v;
              // vector gets filled here....
              for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
              {
              // iteration code here...
              }

              ...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?

              P Offline
              P Offline
              Peter_in_2780
              wrote on last edited by
              #6

              As Richard and Luc have explained, there is no difference in this case. What they MAY have been referring to is this:

              a[i++] = x;
              b[++j] = z;

              In a primitive CPU, the code might compile to something like this (much liberties taken in pseudo assembler code!)

              load r1,i
              add r1,=a
              load r2,x
              store r2,(r1)
              load r1,i
              inc r1
              store r1,i

              load r1,j
              inc r1
              store r1,j
              add r1,=b
              load r2,z
              store r2,(r1)

              In the early days of C, there were machines that stupid, but by the time C++ came along, the "issue" was long gone. Essentially, it's about using ++i in an expression compared to using i++. If the value of the side effect isn't used, there's no real difference. Cheers, Peter

              Software rusts. Simon Stephenson, ca 1994.

              1 Reply Last reply
              0
              • X Xpnctoc

                I was reading a coding style document put out by a certain company, when I came across an odd claim in their justification for their FOR loop style. I was wondering if someone could shed some light on this for me: The document states that a pre-increment operator is preferable to a post-increment operator because post-increment introduces potential inefficiencies when iterating over an object. The example they give is C++ code:

                std::vector v;
                // vector gets filled here....
                for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i)
                {
                // iteration code here...
                }

                ...and an identical sample that uses "i++" instead of "++i" for the increment operation. I see no difference in the efficiency. Whether using ++i or i++, "i" is going to contain its new value before the termination condition is evaluated. The loop will be iterated an identical number of times. I was wondering if someone could shed some light on what it is that I'm missing here, or if perhaps the assertion that pre-increment is more efficient. Is there some other language or platform that treats identical FOR loop sytax in a wildly different manner?

                D Offline
                D Offline
                dasblinkenlight
                wrote on last edited by
                #7

                Pre-increment's return value is the iterator itself, while post-increment must return a copy of the iterator before it has been incremented. Since the for loop discards the return value, using the pre-increment version of the ++ operator saves some CPU cycles by not making a copy that will be discarded anyway. A longer explanation is here.[^] Edit: Oops, I did not see that there were so many replies already. Only the OP was visible at the bottom of the forums window.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups