Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: post/pre increment operator speed

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    United Kingdom
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default post/pre increment operator speed

    Hi, i was wonder if the expression:
    ++j
    is faster than
    j++
    Let's think to a for cycle:
    for(j=0; j<array.length; ++j) {
    foo.staff();
    }
    at the last cycle, after the foo.staff() execution, what will happen will be that j will be incremented before to be considered for the condition. It should be different from the other expression, as in the case of j++, first j would be considered and then incremented.
    WFYI(Waiting for your impressions).


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Difference ++j and J++

    I don't know how exactly you mean by faster....
    As far as efficiency is concerned, both are equal. Both has the efficiency level constant in terms of Anaylsis.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: post/pre increment operator speed

    Please don't hijack someone else's thread, I've moved your question into it's own thread.

    The two pieces of code run the loop different number of times so it's not a fair comparison.

    As far as ++j or j++ itself is concerned, the Java compiler is smart enough to make sure these statements will both execute in more or less the same time.

    The same can't be said for languages which allow operator overloading (e.g. C++), and it depends on the implementation.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: post/pre increment operator speed

    What happened when you wrote a small test program that profiled each operation for you? Say, do each a million times and see how long it takes?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. post increment
    By deependeroracle in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2012, 11:31 AM
  2. how to wrap a pre-generics library
    By frumious in forum Object Oriented Programming
    Replies: 2
    Last Post: April 9th, 2012, 09:58 PM
  3. Help with using pre-created objects and Vectors **First Post :)**
    By jimmyo88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 18th, 2011, 08:08 AM
  4. Inserting pre programmed UI elements
    By luisp88 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 20th, 2011, 12:56 PM
  5. Need...more...speed!
    By nPeep in forum Java Networking
    Replies: 8
    Last Post: September 6th, 2010, 11:07 AM