Comments on: Do you really know how for-loops work? How to speed up your for loops Friends of Oracle and Java Sat, 01 Aug 2015 11:10:05 +0000 hourly 1 By: Infernoz Thu, 02 Aug 2007 00:43:28 +0000 This is such basic stuff, that it should not need a blog article, but I see so many idiots do this!

Much like the frequent and painful abuse of String in Java, like:
String s = “”
for (int i=0; i=0; ) {} //Note that i does not go negative inside the loop.

By: HuSoft Wed, 18 Jul 2007 16:00:16 +0000 You could also do:

for (var i = 0, len = document.getElementById(‘t’).getElementsByTagName(‘td’).length; i

By: Nishant Saini Wed, 18 Jul 2007 12:19:19 +0000 Nice Article

By: Fikovnik Wed, 18 Jul 2007 10:18:33 +0000 it did completely messed up my comments :(

By: Fikovnik Wed, 18 Jul 2007 10:17:08 +0000 Even in java:

for (int i=0; i=0; i--) {} // comparing with 0 is faster but it leads to slightly not very clean code and the difference is not big ;)<br />&nbsp;

and sure most of the time iterators and enhanced for loops are being used anyway

By: p3t0r Tue, 17 Jul 2007 08:54:04 +0000 The JVM _can_ actually determine if the value could change… but since you’re probably using unsynchronized collections the size of a collection can actually change during your for loop. I’ve seen this latter happening inside Hibernate where they use your proposed construction; it’s faster but with a trade-of.

By: Matt Giuca Mon, 16 Jul 2007 12:17:55 +0000 “And this doesn’t look like an expensive operation in my opinion.”

Well, “document.getElementById(‘t’).getElementsByTagName(‘td’).length” is quite an expensive operation, particularly in JavaScript. It’s about as expensive an operation as you’re likely to find on the end of a for loop! Remember that even looking up an identifier in JavaScript needs to consult a hash table. So, the above expression would have to:

* Lookup “document” in the global identifiers.
* Lookup “getElementById” in the document object’s dictionary.
* Execute the “getElementById” function, passing ‘t’ as an argument.
* Look throughout the document for an element with the id ‘t’. This is rather expensive, depending on how the browser indexes element IDs (could be cheap).
* Check to make sure the returned object is not null; if so throw an exception.
* Lookup “getElementsByTagName” on the returned object’s dictionary.
* Execute the “getElementsByTagName” function, passing ‘td’ as an argument.
* The killer: Search the entire document tree, doing a string comparison against _every_ element’s tag name, and doing a list insert for all successful elements. The browser could intelligently hash tag names but I don’t think they do.
* Finally lookup “length” in the returned list object’s dictionary.
* And execute the “length” function, which cheaply returns the length as an integer.

So it is rather a good example then, of the perfect expression which deserves to be thrown with great vigour out of the loop body!

By: Jeroen van Wilgenburg Mon, 16 Jul 2007 12:01:35 +0000 Thanks prashant.
>> sezgin: How can the JVM know that value of the method you’re calling never changes? And when the JVM optimizes this I should’ve got other test results. I’m sure the JVM optimizes certain things, but it is better when you optimze the for loops yourself, because the JVM doesn’t know everything.

By: prashant Mon, 16 Jul 2007 10:57:45 +0000 Hi,

Good simple article.I liked it and am also not aware of this till now having almost 3.7 yrs experience. :-)


By: sezgin Mon, 16 Jul 2007 00:02:57 +0000 JVM optimizes this kind of things .So I don’t worry about that for Java.But worry for javascript.