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

for-loop variable initialisation

D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
I've been looking at sorting algorithms (just for fun), and even came up with one that I didn't find in the list of "known algorithms". Its a variant of insertion sort (but thats not of importance here). Its far from a good algorithm, but I observed a strange thing with it.

The first variant is how I wrote it originally, the second variant is how I began to re-write it now.

Basically I only pulled out some calculation from the loop tests, into loop variables. I expected the second form to run marginally faster, but to my surprise it runs significantly slower (for a long[] with 100000 elements its almost 1sec slower on my system). I'm wondering if anyone has an explanation for this behaviour?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Did you run each sort only once, or multiple times? Try timing the sorting an array of 100,000 elements five times in a row with each algorithm (obviously you need a new array of unsorted longs each time.) I could almost guarantee that by the third or fourth run through in a single running program, each algorithm will be taking the same amount of time. Initially, the times may be a bit different, but once the Hotspot engine optimizes the code, they should both come out just the same.

[Jess in Action][AskingGoodQuestions]
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Hmm, you were right. With four runs each, the total amount of time was 1sec different, instead of ~4sec. With 9 runs, the difference in total time was negligible (with the second algorithm slightly in front). Thanks for the hint!

Still think its strange. If the bytecode of one form is shorter (less instructions to do), I would expect the JVM to execute the code faster BEFORE any internal optimizations. Unless operations like loading variables take longer than arithmetic operations..
I agree. Here's the link:
subject: for-loop variable initialisation
It's not a secret anymore!