i++, ++i, i+=1 and i = i+1;

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jim

    i++, ++i, i+=1 and i = i+1;

    Hello,
    let say we have;

    1) i++; /* use i and increment by one */
    2) ++i; /* increment i by one and use it */
    3) i += 1;
    4) i = i+1;

    result (for value of i) of all 4 will be same; could anyone tell
    differences among them from any perspectives?
    I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).

    Thanks in advance,

  • Richard Heathfield

    #2
    Re: i++, ++i, i+=1 and i = i+1;

    jim said:
    Hello,
    let say we have;
    >
    1) i++; /* use i and increment by one */
    2) ++i; /* increment i by one and use it */
    3) i += 1;
    4) i = i+1;
    >
    result (for value of i) of all 4 will be same; could anyone tell
    differences among them from any perspectives?
    I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
    Use the one you think most clearly conveys your intent. Premature
    optimisation is the root of all evil, as Donald Knuth rightly said.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at the above domain, - www.

    Comment

    • Random832

      #3
      Re: i++, ++i, i+=1 and i = i+1;

      2006-12-19 <e8CdnawbeL27oh XYRVnytwA@bt.co m>,
      Richard Heathfield wrote:
      jim said:
      >
      >Hello,
      >let say we have;
      >>
      >1) i++; /* use i and increment by one */
      >2) ++i; /* increment i by one and use it */
      >3) i += 1;
      >4) i = i+1;
      >>
      >result (for value of i) of all 4 will be same; could anyone tell
      >differences among them from any perspectives?
      >I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
      >
      Use the one you think most clearly conveys your intent. Premature
      optimisation is the root of all evil, as Donald Knuth rightly said.
      Note that 1 does have a meaning slightly different than 2 3 or 4, if
      used in an expression.

      Any compiler worth anything will compile all four [in a statement by
      themselves, and 2 3 4 anywhere in an expression] to exactly the same
      thing - so, yeah, use whichever one is most readable. And "i+++1"
      instead of "++i" is just silly.

      Comment

      • Stephen Sprunk

        #4
        Re: i++, ++i, i+=1 and i = i+1;

        "jim" <jimyoo1@gmail. comwrote in message
        news:1166556524 .397567.30780@t 46g2000cwa.goog legroups.com...
        Hello,
        let say we have;
        >
        1) i++; /* use i and increment by one */
        2) ++i; /* increment i by one and use it */
        3) i += 1;
        4) i = i+1;
        >
        result (for value of i) of all 4 will be same; could anyone tell
        differences among them from any perspectives?
        I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
        If those lines are the entirety of the relevant expressions, there is
        absolutely no difference to modern compilers. One may express your
        intent better than the others, which may help other folks understand
        your code, but they'll end up the same when compiled.

        It used to be true, back in the 70s and 80s, that some forms were faster
        than others because compilers weren't very good at optimizing, and such
        advice was actually helpful. Someone spouting the same advice today,
        however, is merely propogating urban legends; beware of anything you get
        from such a source.

        S

        --
        Stephen Sprunk "God does not play dice." --Albert Einstein
        CCIE #3723 "God is an inveterate gambler, and He throws the
        K5SSS dice at every possible opportunity." --Stephen Hawking


        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        • Peter Nilsson

          #5
          Re: i++, ++i, i+=1 and i = i+1;

          jim wrote:
          Hello,
          let say we have;
          >
          1) i++; /* use i and increment by one */
          2) ++i; /* increment i by one and use it */
          3) i += 1;
          4) i = i+1;
          >
          result (for value of i) of all 4 will be same;
          Better to say the side effect will be the same, the result of the
          expression is clearly
          different for the first case.
          could anyone tell differences among them from any perspectives?
          As stand alone statements, there's none.
          I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
          You most likely heard that in a C++ context with particular reference
          to
          i being an iterator. Note that C and C++ are different languages with
          different paradigms and you'll only confuse yourself if you mix the
          two. [From the question I'm assuming you're a relative newbie.]

          So if you're currently learning C++, I suggest you hop over to a C++
          group. Note that this question is almost certainly a FAQ though.

          If you're currently learning C but have been reading C++ material
          in the belief that it's similar, DON'T!

          --
          Peter

          Comment

          • pete

            #6
            Re: i++, ++i, i+=1 and i = i+1;

            Random832 wrote:
            >
            2006-12-19 <e8CdnawbeL27oh XYRVnytwA@bt.co m>,
            Richard Heathfield wrote:
            jim said:
            Hello,
            let say we have;
            >
            1) i++; /* use i and increment by one */
            2) ++i; /* increment i by one and use it */
            3) i += 1;
            4) i = i+1;
            >
            result (for value of i) of all 4 will be same; could anyone tell
            differences among them from any perspectives?
            I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
            Use the one you think most clearly conveys your intent. Premature
            optimisation is the root of all evil, as Donald Knuth rightly said.
            >
            Note that 1 does have a meaning slightly different than 2 3 or 4, if
            used in an expression.
            4 can have a different meaning from the other three
            if i is a macro with side effects.

            --
            pete

            Comment

            • August Karlstrom

              #7
              Re: i++, ++i, i+=1 and i = i+1;

              jim skrev:
              let say we have;
              >
              1) i++; /* use i and increment by one */
              2) ++i; /* increment i by one and use it */
              3) i += 1;
              4) i = i+1;
              >
              result (for value of i) of all 4 will be same; could anyone tell
              differences among them from any perspectives?
              I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
              As Peter mentioned, you most likely heard that in a C++ context where
              the increment operation is being overloaded. Try to implement the
              following functions to get a feeling for the difference between 1 and 2:

              /* Increment the integer pointed at and return the new value. */
              int preinc(int *p);

              /* Increment the integer pointed at and return the old value. */
              int postinc(int *p);


              August

              Comment

              • malc

                #8
                Re: i++, ++i, i+=1 and i = i+1;

                Richard Heathfield <rjh@see.sig.in validwrites:
                jim said:
                >
                >Hello,
                >let say we have;
                >>
                >1) i++; /* use i and increment by one */
                >2) ++i; /* increment i by one and use it */
                >3) i += 1;
                >4) i = i+1;
                >>
                >result (for value of i) of all 4 will be same; could anyone tell
                >differences among them from any perspectives?
                >I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
                >
                Use the one you think most clearly conveys your intent. Premature
                optimisation is the root of all evil, as Donald Knuth rightly said.
                http://en.wikipedia.org/wiki/Optimiz...mputer_science)
                Third paragraph. I think the credit must go where it's due.

                --
                vale

                Comment

                • websnarf@gmail.com

                  #9
                  Re: i++, ++i, i+=1 and i = i+1;

                  jim wrote:
                  Hello,
                  let say we have;
                  >
                  1) i++; /* use i and increment by one */
                  2) ++i; /* increment i by one and use it */
                  3) i += 1;
                  4) i = i+1;
                  result (for value of i) of all 4 will be same; could anyone tell
                  differences among them from any perspectives?
                  In isolation there is no difference between any of them.

                  On the other hand, if you use any of them as the component of an
                  expression, then the first one can be different.

                  int i, j;

                  i = 2;
                  j = i++; /* j will become 2, i will become 3 */

                  i = 2;
                  j = ++i; /* j will become 3, i will become 3 */

                  i = 2;
                  j = (i += 1); /* j will become 3, i will become 3 */

                  i = 2;
                  j = (i = i + 1); /* j will become 3, i will become 3 */

                  There are more implications for what all this means for C++ and its
                  operator overloading feature, however that's another suject entirely.
                  There are probably some implications depending on if i is declared
                  volatile and sig_atomic_t as well, but I'm too lazy to work them all
                  out.
                  I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
                  Its not *better* to use any of them. There is a subtle difference in
                  them so you should use one that is appropriate to your purpose. C
                  abstracts them all to nearly the same thing at compile time, so there
                  is no reason for their to be any speed difference in any of them
                  (matters are slightly different for C++ operator overloading.)

                  --
                  Paul Hsieh
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



                  Comment

                  • Richard Heathfield

                    #10
                    Re: i++, ++i, i+=1 and i = i+1;

                    malc said:
                    Richard Heathfield <rjh@see.sig.in validwrites:
                    >
                    >jim said:
                    >>
                    >>Hello,
                    >>let say we have;
                    >>>
                    >>1) i++; /* use i and increment by one */
                    >>2) ++i; /* increment i by one and use it */
                    >>3) i += 1;
                    >>4) i = i+1;
                    >>>
                    >>result (for value of i) of all 4 will be same; could anyone tell
                    >>differences among them from any perspectives?
                    >>I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
                    >>
                    >Use the one you think most clearly conveys your intent. Premature
                    >optimisation is the root of all evil, as Donald Knuth rightly said.
                    >
                    http://en.wikipedia.org/wiki/Optimiz...mputer_science)
                    Third paragraph. I think the credit must go where it's due.
                    Hmmm. What makes you think the Wikipedia article is correct? After all, if
                    you look up their C stuff, you'll find that they confuse arguments and
                    parameters at least twice, claim that * is a type qualifier, and assume
                    that all platforms use ASCII.

                    We all know that *anyone* can edit Wikipedia, regardless of their level of
                    knowledge of the subject matter, so why treat the Wiki like some kind of
                    oracle? It's only as good as the last person to edit it. If that was
                    someone who happens to know his or her stuff, you get good information. And
                    if it wasn't, you get bad information.

                    So the question a Wiki reader has to ask himself is: "do I feel lucky?"

                    --
                    Richard Heathfield
                    "Usenet is a strange place" - dmr 29/7/1999

                    email: rjh at the above domain, - www.

                    Comment

                    • Guest's Avatar

                      #11
                      Re: i++, ++i, i+=1 and i = i+1;

                      "jim" <jimyoo1@gmail. comwrote in message
                      news:1166556524 .397567.30780@t 46g2000cwa.goog legroups.com...
                      Hello,
                      let say we have;
                      >
                      1) i++; /* use i and increment by one */
                      2) ++i; /* increment i by one and use it */
                      3) i += 1;
                      4) i = i+1;
                      >
                      result (for value of i) of all 4 will be same; could anyone tell
                      differences among them from any perspectives?
                      I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
                      >
                      Thanks in advance,
                      >
                      I was told that in case 3) "i" is accessed once and modified once;
                      in case 4) "i" is accessed twice and modified once.

                      Someone else could hopefully say if it is true and what it means in reality.

                      /Krister


                      Comment

                      • David T. Ashley

                        #12
                        Re: i++, ++i, i+=1 and i = i+1;

                        "jim" <jimyoo1@gmail. comwrote in message
                        news:1166556524 .397567.30780@t 46g2000cwa.goog legroups.com...
                        Hello,
                        let say we have;
                        >
                        1) i++; /* use i and increment by one */
                        2) ++i; /* increment i by one and use it */
                        3) i += 1;
                        4) i = i+1;
                        Any modern compiler is going to recognize them all as an increment on an
                        integer, and the generated code will be the same.

                        However, if one tries to use the value of the expression ... that is a
                        different matter.

                        The four statements:

                        j = (i++);
                        j = (++i);
                        j = (i += 1);
                        j = (i = i+1);

                        will give two different results.

                        Most people will use "i++" for simple iteration, as in:

                        for (i=0; i<10; i++)
                        do_whatever();

                        or

                        while(pointer)
                        {
                        val = *pointer;
                        pointer++;
                        }



                        Comment

                        • malc

                          #13
                          Re: i++, ++i, i+=1 and i = i+1;

                          Richard Heathfield <rjh@see.sig.in validwrites:
                          malc said:
                          >
                          >Richard Heathfield <rjh@see.sig.in validwrites:
                          >>
                          [..snip..]
                          >>Use the one you think most clearly conveys your intent. Premature
                          >>optimisatio n is the root of all evil, as Donald Knuth rightly said.
                          >>
                          >http://en.wikipedia.org/wiki/Optimiz...mputer_science)
                          >Third paragraph. I think the credit must go where it's due.
                          >
                          Hmmm. What makes you think the Wikipedia article is correct? After all, if
                          you look up their C stuff, you'll find that they confuse arguments and
                          parameters at least twice, claim that * is a type qualifier, and assume
                          that all platforms use ASCII.
                          Well, i knew that i have seen discussions on the subject in the past,
                          decided to check with wikipedia and there it was. Furthermore:



                          yields a lot of hits.

                          All that said, cursory look on the paper where this quote is taken
                          from (and out of context if i might add) does not suggest that Knuth
                          is quoting Hoare.

                          Structured Programming with go to Statements

                          DONALD E. KNUTH

                          Stanford University, Stanford, California 94305

                          page 268

                          <quote>
                          We should forget about small efficiencies, say about 97% of the time:
                          premature optimization is the root of all evil.
                          </quote>

                          The first part of the quote makes the rest of it a lot less of a
                          sweeping generalization.

                          After spending some time searching the web i have no definitive proof
                          that Knuth was quoting Hoare, so sorry about that.

                          Then again taking less than a half of a sentence and using that as an
                          appeal to authority is not a right thing to do.
                          >
                          We all know that *anyone* can edit Wikipedia, regardless of their level of
                          knowledge of the subject matter, so why treat the Wiki like some kind of
                          oracle? It's only as good as the last person to edit it. If that was
                          someone who happens to know his or her stuff, you get good information. And
                          if it wasn't, you get bad information.
                          >
                          So the question a Wiki reader has to ask himself is: "do I feel lucky?"
                          Perhaps.

                          --
                          vale

                          Comment

                          • Richard Heathfield

                            #14
                            Re: i++, ++i, i+=1 and i = i+1;

                            malc said:

                            <snip>
                            >
                            Then again taking less than a half of a sentence and using that as an
                            appeal to authority is not a right thing to do.
                            I suppose you could view it in that light, but I was simply using Knuth's
                            words because they conveyed, pithily and effectively, a point that I myself
                            wanted to make, and of course I ascribed them to the man whom I believed
                            (and believe) to be their author because, well, one does, doesn't one?

                            --
                            Richard Heathfield
                            "Usenet is a strange place" - dmr 29/7/1999

                            email: rjh at the above domain, - www.

                            Comment

                            • Ben Pfaff

                              #15
                              Re: i++, ++i, i+=1 and i = i+1;

                              malc <malc@pulsesoft .comwrites:
                              an appeal to authority is not a right thing to do.
                              I agree.
                              --
                              "This is a wonderful answer.
                              It's off-topic, it's incorrect, and it doesn't answer the question."
                              --Richard Heathfield

                              Comment

                              Working...