View Poll Results: a++ or ++a?

Voters
35. You may not vote on this poll
  • a++

    23 65.71%
  • ++a

    10 28.57%
  • a = a + 1

    1 2.86%
  • a = 1 + a

    0 0%
  • never uses them

    1 2.86%

a++ or ++a

This is a discussion on a++ or ++a within the A Brief History of Cprogramming.com forums, part of the Community Boards category; ^ I agree... but please don't say 'natural'... after another thread I started, I plan on completely deleting the word ...

  1. #16
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^ I agree... but please don't say 'natural'... after another thread I started, I plan on completely deleting the word 'natural' from my vocabulary...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #17
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    How about:

    a += 1;

    I'm tending to use that more and more often. It just looks better and clearer.

  3. #18
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by Speedy5
    How about:

    a += 1;

    I'm tending to use that more and more often. It just looks better and clearer.
    oookkk....
    Do not make direct eye contact with me.

  4. #19
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Unless I am using the return value for something (which is fairly rare) forcing the issue, I prefer to use ++a.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #20
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by Prelude
    >Indeed.
    I think you missed the point. A well known micro-optimization technique is to invert the loop, starting from the end and decrementing. This is on the assumption that some machines have a special instruction for decrementing and comparing with zero and a reversed loop is a good hint to the compiler to use those instructions.
    Yes, but the only difference is a single subtraction operation done by the processor. If you need to optimize things like that, do what Sebastiani said and use asm.

  6. #21
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by Speedy5
    How about:

    a += 1;

    I'm tending to use that more and more often. It just looks better and clearer.
    are you insane? how does
    Code:
    a += 1;
    look better than
    Code:
    a++;
    //or
    ++a;
    i just don't get it...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #22
    .........
    Join Date
    Nov 2002
    Posts
    303
    Originally posted by Speedy5
    How about:

    a += 1;

    I'm tending to use that more and more often. It just looks better and clearer.
    I use that only when it's a number other an 1.

    For just an incremment of one I usually use a++ out of habit, but sometimes just to be "a little crazy" about things I will use ++a hehe.

  8. #23
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    C++, there is nothing better than this.
    Yoshi

  9. #24
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I wonder if there will ever be a language based on C++ called C+=2

  10. #25
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Hey, I made that joke a few weeks ago :P

    I do tend to use ++a (or --a) unless I need the old value. Although any good optimizing compiler should make no distinction, ++a is:

    * As readable as a++
    * Never worse in performance than a++ and sometimes better

    I agree, increment/decrement is not the place to look at optimizing, but this is not your typical optimization, because there is no benefit from a++ if you don't need the old value. Optimization is all about making decisions based on tradeoffs. For example, do you want a fast program or a small one? Do you want to take advantage of processor-specific features that might slow your program down on other systems? Do you want to keep your classes very uncoupled, making code changes easier and bugs rarer, or do you need the added speed that tighter coupling can provide?

    In this case, if there is only one side of the story -- if ++a is always as good and possibly better (at least in cases where ++ has the typical meaning), then I think ++a is the way to go.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #26
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    a++ is much faster than ++a because its easier for the 'a' to push the ++ forward in the program rather than pull it. dont you agree that pushing is easier than pulling?

  12. #27
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    pulling is much easier when your in practise
    "Assumptions are the mother of all **** ups!"

  13. #28
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    According to the discussion of whether ++a is faster than a++ I have made a few tests. i compiled the test program below (without optimizations) and got the following results:


    EDIT:
    Old code, see next page for updated test



    As you can see, not much differs from them. They have the same instructions run, only in a different order. My conclusion from this is they are both running at the same speed. If someone else have anything to add, please do.

    EDIT: Another observation. In a = a++ there is this line:
    Code:
    mov	eax, DWORD PTR _a$[ebp]
    mov	DWORD PTR _a$[ebp], eax
    This could be optimized away, thus making a=a++ faster.
    Last edited by Magos; 11-29-2003 at 11:31 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  14. #29
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >This could be optimized away, thus making a=a++ faster.
    Why would you want to optimize undefined behavior?
    My best code is written with the delete key.

  15. #30
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    > Why would you want to optimize undefined behavior?
    Well would you want it slow... There's no fun in that.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21