aspose file tools*
The moose likes Java in General and the fly likes Faster between Increment and Decrement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Faster between Increment and Decrement" Watch "Faster between Increment and Decrement" New topic
Author

Faster between Increment and Decrement

Rajesh Kumar Swain
Greenhorn

Joined: Apr 01, 2007
Posts: 17
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    I was still surprised when I tried the whole thing again.
    Rajesh Kumar Swain
    Greenhorn

    Joined: Apr 01, 2007
    Posts: 17
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    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.
    Peter Chase
    Ranch Hand

    Joined: Oct 30, 2001
    Posts: 1970
    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.


    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
    Rajesh Kumar Swain
    Greenhorn

    Joined: Apr 01, 2007
    Posts: 17
    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.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 19064
        
      40

    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


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    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.
    Stefan Wagner
    Ranch Hand

    Joined: Jun 02, 2003
    Posts: 1923

    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.


    http://home.arcor.de/hirnstrom/bewerbung
    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12835
        
        5
    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

    Joined: Apr 01, 2007
    Posts: 17
    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
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42926
        
      68
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    I have come to the conclusion that the first thing I wrote on this thread was correct.
    Stefan Wagner
    Ranch Hand

    Joined: Jun 02, 2003
    Posts: 1923

    The conclusion is:
    The result isn't interesting, but the arguments are.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Faster between Increment and Decrement