wood burning stoves 2.0*
The moose likes Performance and the fly likes Performance by Loop optimisation -?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Performance
Bookmark "Performance by Loop optimisation -??" Watch "Performance by Loop optimisation -??" New topic
Author

Performance by Loop optimisation -??

Balaji Munuswamy
Ranch Hand

Joined: Apr 07, 2002
Posts: 37
I have two methods which does the same operation, but with a slight difference in for loop..
METHOD I
--------
public void loopcontrol()
{
String str = "abcdefghijklmnopqurstuvwxyz";
long st = System.currentTimeMillis();
System.out.println("Starting Time = "+st);
int len = str.length();
for (int j = 0; j < len; j++) {
System.out.println(j);
}
long endt = System.currentTimeMillis();
System.out.println("Finishing Time = "+endt);

System.out.println("Total No of MilliSeconds to perform the opertaion = "+(endt-st));
}
METHOD II
---------
public void secondLoopcontrol()
{
String str = "abcdefghijklmnopqurstuvwxyz";
long st = System.currentTimeMillis();
System.out.println("Starting Time = "+st);
int len = str.length();
for (int j = len -1 ; j >= 0; j--) {//for loop alone changed
System.out.println(j);
}
long endt = System.currentTimeMillis();
System.out.println("Finishing Time = "+endt);

System.out.println("Total No of MilliSeconds to perform thre opertaion = "+(endt-st));
}
When i execute it using main method, the first method took 110 milliseconds while the second method took just 30 milliseconds.
Can anyone explain what can be the possible reason behind this ???
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1134

Hi Balaji,
As far as I know, this test:

is a lot faster for the computer to execute than:

In fact, I usually use the following "for" loop construct (which I saw in an article by Jack Shirazi):

Hope this helps.
Good Luck,
Avi.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I doubt there's really that much difference in performance for the two loops - the System.out.println() will take notably longer than any difference between >= 0 and < len. Try reversing the order of the two tests you run - I bet you're observing a JVM startup effect of some sort. Personally I get 30 ms for either loop...
[ April 29, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Karan V Praba
Ranch Hand

Joined: Oct 31, 2002
Posts: 41
Yes JIm,
It will not be much difference.Bcoz in my PC Second one has took 16 millsecs where first one completed with 0 millsecs.


Karan V Praba.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
How sure are we that our measurements aren't affected by the JIT compiler kicking in?
- Peter
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yeah, that's why I suggested reversing the order of the tests. Also useful - run each test repeatedly, and see how/if the result changes. If you want to study the JVM startup effects specifically, you need to start a new JVM for each test. I'm usually too lazy to do this, so I just make a main() method that runs the tests enough times that I'm reasonably convinced that startup effects have gone away:

In some cases, you may even see that the perfomance for the final loopcontrol() has gotten worse than it was back when it was executed repeatedly; it seems the JVM may "forget" the optimizations it made back then. You probably won't see that sort of effect here, but I believe I've seen it in some other cases. Hard to really track down that sort of thing though...
[ April 30, 2003: Message edited by: Jim Yingst ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I should acknowledge that yes, >= 0 can be faster on some systems than < len is. It's not a bad hadit to get into, for those loops where it doesn't impede readability to much. I just don't think it's effect is very significant, most of the time. I/O for example will almost always take longer.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1134

Jim,
For what it's worth (and in case you haven't already seen it), this installment from JavaWorld's Java Q&A column, enforces your claims (in my opinion):
http://www.javaworld.com/javaworld/javaqa/2003-04/01-qa-0411-hotspot.html
Cheers,
Avi.
Jack Shirazi
Author
Ranch Hand

Joined: Oct 26, 2000
Posts: 96
If there is a prospect that you are running loops that last for days, or that you might be using non-JIT JVMs, such as embedded or mobile, then you might worry about this technique. Otherwise I'd be surprised if you saw any measurable difference.
The increasing increment is almost always more readable. The time taken to execute the test is the critical factor in the time taken by tight loops (though for this example Jim is, of course, right that the print is the major overhead). Theoretically, since Java has special bytecodes for comparison against -1, 0, 1, 2, 3, 4, 5, then it is possible that the test can be faster in some JVM/system combinations. But the difference is small because it is only effectively the use of one extra CPU register for the less efficient comparison.
On the other hand, if you don't care that the loop is slightly less readable, you can be fairly sure that the comparison to zero will not be less efficient on any system. Though because there is no cost model for bytecodes even this is not guaranteed, just extremely likely.
--Jack Shirazi
JavaPerformanceTuning.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performance by Loop optimisation -??