• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Performance by Loop optimisation -??

 
Balaji Munuswamy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Karan V Praba
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How sure are we that our measurements aren't affected by the JIT compiler kicking in?
- Peter
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic