It's not a secret anymore!*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Collections: Chapter 2: Decrement Performance" Watch "Java Collections: Chapter 2: Decrement Performance" New topic
Author

Java Collections: Chapter 2: Decrement Performance

Yuri Gadow
Greenhorn

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
Author
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="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>
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
Sheriff

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 frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Collections: Chapter 2: Decrement Performance
 
Similar Threads
Hello! New here, question understanding some code
Numerology
Does SingleThreadModel ensure thread-safety?
Triangle Numbers Divisors
performance question: count(*) vs count(<columnName>)