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?
Joined: Mar 29, 2001
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="http://www.amazon.com/exec/obidos/ASIN/189311578X/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">"Definitive Guide to Swing for Java 2"</a>,<br /><a href="http://www.amazon.com/exec/obidos/ASIN/1893115925/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Java Collections"</a> and <a href="http://www.amazon.com/exec/obidos/ASIN/1893115984/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Learn Java with JBuilder 6"</a>
Joined: May 05, 2000
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.
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.