• 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

Faster between Increment and Decrement

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two snippets as follows,

for (int i=0;i<1000 ;i++ )
{
System.out.println("The Number is :- "+i);
}


AND

for (int j=1000;j>0 ;j-- )
{
System.out.println("The Number is :- "+j);
}

I just want to know which one is faster and how ?

can anyone help me regarding this.

Thanks
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch. There is a simple answer to that question:-
Don't know.

Two possible approaches:-
  • Go through it and work out how many operations each loop involves. I suspect they are the same in both cases, so both would execute at the same speed.
  • Around each loop put a line like in the "code" I have suggested below. Run it several times

  • The repeat the exercise with the println statements commented out. I tried it. And I was very surprised by the result.
    [ April 01, 2007: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I was still surprised when I tried the whole thing again.
     
    Rajesh Kumar Swain
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    May I know the reason of surprise as When I have tried this code the result was

    total time for increment(in ms) 2000
    total time for decrement(in ms) 3688

    So the result clearly shows that Increment is much more faster than the decrement.I am also confused.
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I was surprised that increment was faster first time, then surprised that decrement was faster onthe next run!

    Try it the other way round. Decrement first. Try it several times. My PC is obviously quicker than yours; running it from a floppy disc gives various results, but usually whichever runs first is faster, maybe 0.7 seconds as against 0.8 seconds. The times are more variable if I am running on Linux than on Windows. One example here appears to show decrement as faster than increment:-


    . . .
    The Number is :- 8
    The Number is :- 7
    The Number is :- 6
    The Number is :- 5
    The Number is :- 4
    The Number is :- 3
    The Number is :- 2
    The Number is :- 1
    Increment: The duration was 105290.247 microseconds
    Decrement: The duration was 47726.541 microseconds

    Increment: The duration was 19.517 microseconds
    Decrement: The duration was 17.049 microseconds
    . . .
    The Number is :- 10
    The Number is :- 9
    The Number is :- 8
    The Number is :- 7
    The Number is :- 6
    The Number is :- 5
    The Number is :- 4
    The Number is :- 3
    The Number is :- 2
    The Number is :- 1
    Increment: The duration was 88105.752 microseconds
    Decrement: The duration was 62386.310 microseconds

    Increment first.
    Increment: The duration was 19.983 microseconds
    Decrement: The duration was 17.433 microseconds
    . . .
    The Number is :- 4
    The Number is :- 3
    The Number is :- 2
    The Number is :- 1
    Increment: The duration was 82340.233 microseconds
    Decrement: The duration was 67237.303 microseconds

    Decrement: The duration was 17.114 microseconds
    Decrement first.
    Increment: The duration was 19.743 microseconds

    The smaller figures show what happenw when the "println" statements are omitted.

    Without the "println" statements decrement is slightly faster than increment.

    Conclusions:-
  • The actual time to execute depends on the computer, the JVM and the platform.
  • There is something else introducing delays into the execution time. Maybe garbage collection.
  • We cannot predict what will improve performance.
  • There was a recent Sun Developers' Network newsletter which includes this link; look up the bits in it about "dumb code" and "optimization," which suggest that one cannot write "fast" code by hand.
    [ April 02, 2007: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Even better: most of the times I ran the app on Windows increment was faster; on the three occasions I ran it on Linux and copied and pasted decrement was faster. So it is very unpredictable.
     
    Ranch Hand
    Posts: 1970
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This isn't an April Fool, is it? If so, I'm about to look stupid by falling for it...

    If I understand correctly, you are trying to measure the tiny (if any) difference between two types of loop, when the loop contains console I/O. There is no chance of getting a useful answer from that, as the console I/O will swamp (by orders of magnitude) the difference (if there is one) between the two types of loop.

    In C programming, the decrementing loop is supposed to go quicker. If I recall correctly, that may be because processors typically set a flag for "value reached zero", as part of a decrement operations, eliminating the need for extra operations to test the value. I am not sure if such arguments apply in Java.
     
    Rajesh Kumar Swain
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hey Campbell,

    I have tried 10 times repeatedly but I have not marked any such difference
    as I have got the result as follows.

    . Increment is always faster than decrement.
    . Yes if the time for increment increses(i.e from 2000 ms to 3000 ms) time for decrement also increses approximately in the same ratio.


    But according to you time varies sometimes increment is faster and sometimes decrement is faster.

    I just want to know is there any reason other than console I/O explained by
    Peter Chase.

    Thanks Campbell for your valuable time.
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Just did a quick test (with no I/O). And it is identical -- within a few milliseconds of each other. Here is the code, if you are interested in trying it out yourself:



    Henry
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Posted by Peter Chase:

    This isn't an April Fool, is it?

    You're only allowed April Fools before noon. And what I wrote wasn't.

    I agree it must be console IO producing most of the differences, but even when I left out the println statements there was still variability. Might be co-processes, or it might simply reflect "random" variation in the processing speed from one execution to another.
     
    Ranch Hand
    Posts: 1923
    Scala Postgres Database Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You could start comparing post- and preincrement too like this:

    and run it with
    - but:
    If nothing is done inside the inner loop, it might get optimized away.
    And if you start doing something inside, it will last much, much longer than a 'i++' or '--i'.

    Using 1 000 000 instead of 1 000 it doesn't need a second to do all 64 tests.
    Do you really need 2000, 3000 ms or 2000, 3000 �s?

    btw.: It's a performance-question and has been asked in the performance-section before.
     
    Author and all-around good cowpoke
    Posts: 13078
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Man! That really takes me back!

    This little "hint" was wandering around the Java world back in the days of version 1.02. For all I know it may have even been true in some primitive implementation.

    I hate to think how much confusing logic has been used over the years in pursuit of "performance improvements" like this. Write your programs for clarity, not the pursuit of some imaginary improvement.

    Bill
     
    Rajesh Kumar Swain
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hey you all,

    I just want the conclusion. I am also getting confused after getting different suggestions from different people. Can any one drive me in a right direction.

    Thanks to all

    Rajesh
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The conclusion seems to be that this depends on so many factors (OS, JRE version, JIT, moon phase, etc.) that there is no general answer. And even if there is one, it should not be the basis of any performance tuning.
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have come to the conclusion that the first thing I wrote on this thread was correct.
     
    Stefan Wagner
    Ranch Hand
    Posts: 1923
    Scala Postgres Database Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The conclusion is:
    The result isn't interesting, but the arguments are.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic