Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Loop unrolling.

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Location
    Pacific Northwest
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Loop unrolling.

    I learned about loop unrolling a long time ago and got the sudden inspiration this morning to try it in action. So i whipped together this little program.

    String s=""; 
    for (int i = 0; i < 10000 ; i++){
      s = s + i;
    }

    I compared the runtime to this guy here.

    for (int i = 0; i < 10000 ; i = i + 10){
      s = s + i;
      s = s + (i+1);
      s = s + (i+2);
      s = s + (i+3);
      s = s + (i+4);
      s = s + (i+5);
      s = s + (i+6);
      s = s + (i+7);
      s = s + (i+8);
      s = s + (i+9);
    }

    I consistently got a longer run time on the second loop. Is this because the compiler simply did a better job of unrolling the loop than I did or I am missing something much bigger?



    Thanks HelloWorld for fixing code blocks. Was just reading about that =)
    Last edited by PenguinLord; July 18th, 2010 at 01:07 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Loop unrolling.

    In a short answer, yes (not sure if it's unrolling the loop, all that matters is it optimizes quite well). A lot of the "old optimization tricks" aren't really necessary when using Java (note: this does not include all, there are some tricks that can have an impact)

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    PenguinLord (July 18th, 2010)

  4. #3
    Junior Member
    Join Date
    Jul 2010
    Location
    Pacific Northwest
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Loop unrolling.

    Thanks for the reply. I had a hunch the compiler was just smarter than I was but I wasn't sure. It has been a while since I programmed much.

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. Need help with do-while loop
    By sk8rrr in forum Loops & Control Statements
    Replies: 2
    Last Post: February 10th, 2010, 07:33 PM
  3. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  4. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM
  5. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM