File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Java Collections: Chapter 2: Decrement Performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Collections: Chapter 2: Decrement Performance" Watch "Java Collections: Chapter 2: Decrement Performance" New topic

Java Collections: Chapter 2: Decrement Performance

Yuri Gadow

Joined: May 08, 2001
Posts: 28
I have a question for Mr. Zukowski (or anyone familiar with the book), regarding chapter two of his book (which I read on the JDC.) In it, he states that counting down (as in for (;;i--)) is faster than counting up, "more significantly with the 1.3 release" (hint). Running your timing example code does, in fact, show numbers that support this hypothesis. However, reversing the order in which the count directions are tested, or better yet testing them separately in different VM instances yields numbers that seem to tell a slightly different story. That story being that whichever loop is run second is significantly faster, regardless of its count direction or the preceding loops direction � assuming HotSpot is on (hint). But perhaps I missed the java.exe option -runFaster?
Seriously though, was this hypothesis not tested to eliminate variables like runtime optimizations and memory profiles - or am I missing something here?
John Zukowski
Ranch Hand

Joined: Mar 29, 2001
Posts: 88
Apparently I didn't test out the timing well enough across platforms. I do have to say that the second version does not always run faster though. I've heard back from someone on Solaris that leaving the order as is, their decreasing Delta numbers came up 4x slower, as the second loop.
John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"

John Zukowski Author of <a href="" target="_blank" rel="nofollow">"Definitive Guide to Swing for Java 2"</a>,<br /><a href="" target="_blank" rel="nofollow">"Java Collections"</a> and <a href="" target="_blank" rel="nofollow">"Learn Java with JBuilder 6"</a>
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
According to Jack Shirazi in the book "Java Performance Tuning":
"Comparison to 0 is faster than comparisons to most other numbers. The VM has optimizations for comparisons to the integers -1, 0, 1, 2, 3, 4, and 5. So rewriting loops to make the test comparison against 0 may be faster."
However, he also adds: "Only non-JIT VMs and HotSpot showed improvements by rewriting the loop."
I ran several tests through a JIT compiler and saw no difference of any kind.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Wierdly enough, the JVM which I (am forced to) use on a DEC Unix Alpha box does support a non-standard "-fast" command line option. It makes writing scripts to work with any VM a pain in the proverbial.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
I agree. Here's the link:
subject: Java Collections: Chapter 2: Decrement Performance
It's not a secret anymore!