Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Performance by Loop optimisation -??

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic