• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

++j faster than j++ ?

 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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),
Marco
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, j++ and ++j are effectively equivalent in this case. Here's the sequence of events, for aray size 3:
j = 0
is j < array.length? yes, 0 < 3, keep looping
foo.staff();
++j
j is 1
is j < array.length? yes, 1 < 3, keep looping
foo.staff();
++j
j is 2
is j < array.length? yes, 2 < 3, keep looping
foo.staff();
++j
j is 3
is j < array.length? no, 3 !< 3, stop looping
If you replace ++j with j++, it makes no difference here - the comparison with array.length is done separately.
The only time ++j vs. j++ makes a difference is if you're doing something else with the expression as part of a larger expression, e.g.
i = j++;
is different from
i = ++j;
but
j++;
i = j;
is not different from
++j;
i = j;
The three parts inside a for statement are separate expressions, and so it doesn't matter where the ++ in the third part is, it won't cross over into the second part.
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, i understood that for the loop correctness nothing changes;
i was just dealing with the 'internal JVM execution time' spent the ++j versus j++. In the latter to add 1 to j, the JVM takes evaluates j, while in the former it add simply 1. Is that correct?
Marco
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should be no differences between j++ and ++j in terms of speed.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We could try to find out the easy way...I repeatedly get about 8800 milliseconds for the first loop and 5700 milliseconds for the second. This might trick one into thinking the second is performing better than the first. When I switch the ordering of the tests (so test i++ and then test ++i) the results of the first loop (which would then be the i++ test) are still about 8800 milliseconds and the results of the second test are still about 5700 milliseconds.
So, the performance of these two operations appears to be equivalent.
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to this:
http://www.javaspecialists.co.za/archive/Issue064.html
there is absolutely no difference!
Avi.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I remembered hearing that old C hackers used ++i instead of i++ because it avoided the creation of a temp variable or something like that. But isn't in nice that javac compiles out any difference...
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic