Post and pre increment

Discussion in 'C Programming' started by kailasam, Jun 14, 2004.

  1. kailasam

    kailasam Guest

    Hello,

    Iam having a doubt. Which is more efficient post increment or Pre increment?
    I have read that preincrement is efficient than Post increment. Iam not able
    to think how it is?
    For an independent statement i++ and ++i which is more efficient?

    Regards

    Kailasam
     
    kailasam, Jun 14, 2004
    #1
    1. Advertising

  2. kailasam

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    kailasam wrote:
    > Hello,
    >
    > Iam having a doubt. Which is more efficient post increment or Pre increment?


    Neither. As far as C is concerned, both are equally effecient.
    As far as the underlying machine language is concerned, it depends on the
    processor and the compiler. But that is off-topic in comp.lang.c




    - --
    Lew Pitcher

    Master Codewright & JOAT-in-training | GPG public key available on request
    Registered Linux User #112576 (http://counter.li.org/)
    Slackware - Because I know what I'm doing.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFAzTGYagVFX4UWr64RAtj2AKDPPiDseqeVwb/9+XBi7Y8ZOusAOACfWXYA
    5fEPwFN0R7FB0Lx6dXccgf8=
    =MhJC
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Jun 14, 2004
    #2
    1. Advertising

  3. kailasam wrote:

    > Which is more efficient post increment or Pre increment?
    > I have read that preincrement is efficient than Post increment.
    > I am not able to think how it is?
    > For an independent statement i++ and ++i which is more efficient?


    This is only important in C++
    where increment operators have been overloaded for *large* objects.
    If you write C++ code as well as C code.
    it is probably a good idea to use the preincrement operator

    ++i;

    instead of the post increment operator.
     
    E. Robert Tisdale, Jun 14, 2004
    #3
  4. kailasam

    Stephen L. Guest

    kailasam wrote:
    >
    > Hello,
    >
    > Iam having a doubt. Which is more efficient post increment or Pre increment?
    > I have read that preincrement is efficient than Post increment. Iam not able
    > to think how it is?
    > For an independent statement i++ and ++i which is more efficient?
    >
    > Regards
    >
    > Kailasam


    If you're not interested in its side effect(s),
    then use whatever fits the readability/style
    of the code you're working in. An optimizing
    compiler _should_ choose the most efficient
    way to implement the operation (you could run
    benchmarks if necessary to see for yourself).

    -
    Stephen
     
    Stephen L., Jun 14, 2004
    #4
  5. kailasam

    kUfa Guest

    > If you're not interested in its side effect(s),
    > then use whatever fits the readability/style
    > of the code you're working in. An optimizing
    > compiler _should_ choose the most efficient
    > way to implement the operation (you could run
    > benchmarks if necessary to see for yourself).


    Well looking the output asm source code might be easier, imo. I personnaly
    prefer not relying on any compiler asumption and use use the preincrement
    operator.

    /David
     
    kUfa, Jun 14, 2004
    #5
  6. kailasam

    Stephen L. Guest

    kUfa wrote:
    >
    > > If you're not interested in its side effect(s),
    > > then use whatever fits the readability/style
    > > of the code you're working in. An optimizing
    > > compiler _should_ choose the most efficient
    > > way to implement the operation (you could run
    > > benchmarks if necessary to see for yourself).

    >
    > Well looking the output asm source code might be easier, imo. I personnaly
    > prefer not relying on any compiler asumption and use use the preincrement
    > operator.
    >
    > /David




    Some compilers don't emit debugging records with
    optimization turned on. This makes it tedious
    to find the _exact_ instructions for the C operation
    in question in the ASM output. Also, with the
    modern CPU architectures, the compiler may be taking
    advantage of parallel execution of ASM instructions
    which look inefficient to the casual observer.


    -
    Stephen
     
    Stephen L., Jun 14, 2004
    #6
  7. kailasam

    Malcolm Guest

    "kailasam" <> wrote
    > Iam having a doubt. Which is more efficient post increment or
    > Pre increment?
    > I have read that preincrement is efficient than Post increment.
    > Iam not able to think how it is?
    >

    You have read about C++. C++ allows overloading of the ++ operator, but the
    form x++ forces the "operator ++" function to return a temporary copy of x
    to maintain the function semantics, whilst the pre-increment ++ form doesn't
    have this problem. For built-in types this not an issue.
    >
    > For an independent statement i++ and ++i which is more
    > efficient?
    >

    Almost certainly both the same, and if there is a difference it would only
    be a cycle or two. However in compound statements you generally need the
    post increment form, because the pointer you are passed or initially
    calculate points to the first object.

    for(i=0;i<N;i++)
    *ptr++ = x;

    This means that it is probably more idiomatic to write "i++" in the for loop
    rather than "++i", though both are essentially equivalent.
     
    Malcolm, Jun 14, 2004
    #7
  8. E. Robert Tisdale wrote:
    > kailasam wrote:
    >
    >> Which is more efficient post increment or Pre increment?
    >> I have read that preincrement is efficient than Post increment.
    >> I am not able to think how it is?
    >> For an independent statement i++ and ++i which is more efficient?

    >
    >
    > This is only important in C++
    > where increment operators have been overloaded for *large* objects.
    > If you write C++ code as well as C code.
    > it is probably a good idea to use the preincrement operator

    Is there any efficiency consideration here?

    >
    > ++i;
    >
    > instead of the post increment operator.


    Regards,
    Krishna
     
    Krishnakumar G, Jun 22, 2004
    #8
  9. kailasam

    Dan Pop Guest

    In <cb8vi5$l7a$> Krishnakumar G <> writes:

    >Is there any efficiency consideration here?


    Nope.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 22, 2004
    #9
  10. On Tue, 22 Jun 2004 15:23:08 +0530, in comp.lang.c , Krishnakumar G
    <> wrote:

    >E. Robert Tisdale wrote:
    >> If you write C++ code as well as C code.
    >> it is probably a good idea to use the preincrement operator


    >Is there any efficiency consideration here?


    Not in C. If you're asking about C++, thats down the hall.

    For future reference ERT is a well-known troll. Consider all his advice as
    suspicious until its confirmed by a regular poster here.

    By the way undoubtedly trollsdale will reply to this post by accusing me of
    being a troll. This is his standard technique.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Jun 22, 2004
    #10
  11. kailasam

    Tom St Denis Guest

    Mark McIntyre wrote:

    > On Tue, 22 Jun 2004 15:23:08 +0530, in comp.lang.c , Krishnakumar G
    > <> wrote:
    >
    >>E. Robert Tisdale wrote:
    >>> If you write C++ code as well as C code.
    >>> it is probably a good idea to use the preincrement operator

    >
    >>Is there any efficiency consideration here?

    >
    > Not in C. If you're asking about C++, thats down the hall.
    >
    > For future reference ERT is a well-known troll. Consider all his advice as
    > suspicious until its confirmed by a regular poster here.


    Which makes me ask why someone would troll a dry group like clc. I mean I
    can see sci.crypt cuz we get all the ex-cia spooks there. But clc?

    No!!! My l33t C standard is the best! Just those stupid snobs at ISO won't
    recognize it... so I'll write thousands of posts about my experience and
    such!!! Real coders use TomSO C+++. It's the real deal.

    Just doesn't quite work as well as the trolls inventing ciphers every 8
    minutes in sci.crypt...

    ;-)

    Tom
     
    Tom St Denis, Jun 23, 2004
    #11
  12. kailasam

    James Kanze Guest

    Mark McIntyre <> writes:

    |> On Tue, 22 Jun 2004 15:23:08 +0530, in comp.lang.c , Krishnakumar G
    |> <> wrote:
    |> >E. Robert Tisdale wrote:

    |> >> If you write C++ code as well as C code. it is probably a good
    |> >> idea to use the preincrement operator

    |> >Is there any efficiency consideration here?

    |> Not in C. If you're asking about C++, thats down the hall.

    More correctly, there might be -- it depends on the hardware and the
    compiler.

    Theoretically, pre-incrementation will be more efficient on most
    processors, for a very naïve code generator. I've never seen a code
    generator that naïve, though; in every case I've seen, if the results
    are not used, the compiler generates exactly the same code for both.

    --
    James Kanze
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
     
    James Kanze, Aug 3, 2004
    #12
  13. kailasam

    James Kanze Guest

    "kUfa" <> writes:

    |> > If you're not interested in its side effect(s),
    |> > then use whatever fits the readability/style
    |> > of the code you're working in. An optimizing
    |> > compiler _should_ choose the most efficient
    |> > way to implement the operation (you could run
    |> > benchmarks if necessary to see for yourself).

    |> Well looking the output asm source code might be easier, imo. I
    |> personnaly prefer not relying on any compiler asumption and use use
    |> the preincrement operator.

    Which, on a PDP-11, is less efficient than post-incrementation:).
    (Unless, of course, the compiler optimizes it.)

    In practice, I've never seen a compiler which didn't do this
    optimization.

    --
    James Kanze
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
     
    James Kanze, Aug 3, 2004
    #13
  14. kailasam

    James Kanze Guest

    "Malcolm" <> writes:

    |> "kailasam" <> wrote
    |> > Iam having a doubt. Which is more efficient post increment or Pre
    |> > increment? I have read that preincrement is efficient than Post
    |> > increment. Iam not able to think how it is?

    |> You have read about C++. C++ allows overloading of the ++ operator,
    |> but the form x++ forces the "operator ++" function to return a
    |> temporary copy of x to maintain the function semantics, whilst the
    |> pre-increment ++ form doesn't have this problem. For built-in types
    |> this not an issue.

    Technically, the issue is exactly the same for built-in types and for
    others. Practically, it is far easier for a compiler to recognize the
    optimization for a built-in type. Practically, however, most compilers
    also recognize it for the overloaded operators in C++ -- the benchmarks
    I've run show no difference between pre and post incrementation,
    regardless of the type.

    |> > For an independent statement i++ and ++i which is more efficient?

    |> Almost certainly both the same, and if there is a difference it
    |> would only be a cycle or two. However in compound statements you
    |> generally need the post increment form, because the pointer you are
    |> passed or initially calculate points to the first object.

    |> for(i=0;i<N;i++)
    |> *ptr++ = x;

    |> This means that it is probably more idiomatic to write "i++" in the
    |> for loop rather than "++i", though both are essentially equivalent.

    I suspect that the reason why i++ is more idiomatic is because it is
    what Kernighan and Richie did.

    --
    James Kanze
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
     
    James Kanze, Aug 3, 2004
    #14
  15. kailasam

    zainlucky

    Joined:
    Dec 5, 2008
    Messages:
    1
    this can be a perfect answer

    if u know something about the working srtucture of computer . u will definately get the answer right.
    the post increment is more efficient . (means A++)

    detail :

    u should first know about the stack . LIFO(last in first out)

    1 . suppose u give this expression to computer 5+++B. (concentrate according to "B");

    LIFO means that the instruction which is last will definately perform first .

    u input 5 and stored in stack (computer know this is integer)
    u enter + and stored in stack (computer know this is a operator)
    u enter + and stored in stack (again an operator)

    now computer knows that if (++) are in stack send them to add function and add 1 and send the result back .

    what u can do with the constants it will add 1 to 5 and what will happen there is nothing to store new value so increment is lost so effciency is lost .
    and then it will come to "B".
    u will finialize that computer is a ****ed up . he is not intellegence like us.

    and if u give this operation B+++5 (concentrate according to "B")

    computer first hold 5 and send it to stack .
    computer will then get + and send it to stack .
    again same
    he know if there are "++" add the the fvalue to the first most variable.
    it will add 1 in the variable value and the will add +5 in it. and gives u the output . now the computer is not confuzed .
    thats all for more detail send me mail on hot,yah.
    user id : zabbas44
    put the server name next to my id (got it)
    bye
    hope u know wts going on.
     
    zainlucky, Dec 5, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eddiew_AUS
    Replies:
    18
    Views:
    643
    Andrey Tarasevich
    Jan 27, 2004
  2. Andreas Sheriff
    Replies:
    10
    Views:
    726
    Jack Klein
    Sep 25, 2004
  3. Replies:
    8
    Views:
    932
    Alf P. Steinbach
    Nov 22, 2007
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    900
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    530
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page