Comments on: Do you really know how for-loops work? How to speed up your for loops https://technology.amis.nl/2007/07/15/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 http://wordpress.org/?v=4.2.3 By: Infernoz https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4826 Thu, 02 Aug 2007 00:43:28 +0000 http://technology.amis.nl/blog/?p=2308#comment-4826 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 https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4825 Wed, 18 Jul 2007 16:00:16 +0000 http://technology.amis.nl/blog/?p=2308#comment-4825 You could also do:

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

]]>
By: Nishant Saini https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4824 Wed, 18 Jul 2007 12:19:19 +0000 http://technology.amis.nl/blog/?p=2308#comment-4824 Nice Article

]]>
By: Fikovnik https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4823 Wed, 18 Jul 2007 10:18:33 +0000 http://technology.amis.nl/blog/?p=2308#comment-4823 it did completely messed up my comments :(

]]>
By: Fikovnik https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4822 Wed, 18 Jul 2007 10:17:08 +0000 http://technology.amis.nl/blog/?p=2308#comment-4822 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 https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4821 Tue, 17 Jul 2007 08:54:04 +0000 http://technology.amis.nl/blog/?p=2308#comment-4821 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 https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4820 Mon, 16 Jul 2007 12:17:55 +0000 http://technology.amis.nl/blog/?p=2308#comment-4820 “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 https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4819 Mon, 16 Jul 2007 12:01:35 +0000 http://technology.amis.nl/blog/?p=2308#comment-4819 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 https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4818 Mon, 16 Jul 2007 10:57:45 +0000 http://technology.amis.nl/blog/?p=2308#comment-4818 Hi,

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

Thanks
Prashant

]]>
By: sezgin https://technology.amis.nl/2007/07/15/do-you-really-know-how-for-loops-work-how-to-speed-up-your-for-loops/#comment-4817 Mon, 16 Jul 2007 00:02:57 +0000 http://technology.amis.nl/blog/?p=2308#comment-4817 JVM optimizes this kind of things .So I don’t worry about that for Java.But worry for javascript.

]]>