Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unrolling loops...  RSS feed

 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I'm curious which how these loopzs would be handled when being compiled. Which one would be unrolled or used to the best performance.

Now I use the 'while' option everywhere for this kind of 'loop' but I've seen the 'for' one all over a project I am working on. Due to it's frequency I then started wondering if maybe the previous programmer knew something I dont.
Of course he left the project.

[This message has been edited by John Bateman (edited March 20, 2001).]
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, I haven't found a compiler that does any optimization to the bytecode. Therefore, the short answer is neither will be unrolled. If any unrolling takes place it will happen at runtime by the JIT or Hotspot. For more see my post here.
There is no real difference between while and for loops. For what I compiled, they were identical:

The bytecode this generated is:

16,17,22 is the for loop and 28,29,34 is the while loop...they are the same.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter.
My next question to you would be "Is there a reference doc that can help properly read the bytecode?"
Please don't tell me 'learn assembler'.
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Bateman:
My next question to you would be "Is there a reference doc that can help properly read the bytecode?"

Yes, there are a couple. I'd look at these:
1) Chapter 6 and 7 of the JVM spec. Chapter 6 is a reference to all the opcodes and chapter 7 shows some Java source code and the bytecode it gets compiled to.
2) I wrote an article on bytecode which can be found here. It is similar to chapter 7 of the JVM spec, but IMHO, more readable and clearer about what's happening.
3) My book. Although I don't explain the details of bytecode in the book as I do in my article referenced above, I show a lot of it and explain various aspects of it related to performance and other things.
I hope this helps,
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!