Results 1 to 4 of 4
  1. #1
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default loop performance (no problem, just investigating)

    In this forum (and others) I've seen quite often questions asked about loop performance.... "is a for loop faster than a while loop?", "... which loop is more efficient?", etc...
    I decided to try to find this out by implementing three different types of loops, for, while and do-while, each doing exactly the same type of tasks and trying to time each of them (notice I said "try").
    The task that I decided that each loop should perform is finding prime numbers (out of 50 millon).
    The results are interesting but I'm not so sure that what I'm seeing is all that true, due to the lack of uniformity I'm seeing.
    Generaly, it goes like this:
    • while loop is the fastest
    • for loop is next
    • do-while loop is last

    I imagine that the different results that I'm seeing is because of other threads running on the computer, for example, sometimes a for loop will have same time spent as the while loop.
    So my questions are:
    • Was the method of timing that I used the most accurante/correct?
    • I'm I correct in my assumption that other threads running in the computer are interfering with the values I'm seeing?

    Here's the code I used (I really didn't try to code it with "correctness"... just wanted to get it working).
    Java Code:
    public class LoopTimer
    {
     static int cycles = 50000000;
     static boolean isPrime = false;
     static int counter=0;
     
     public static void main (String[]arg){
     //The purpose is to time the performance of the different forms of loops. 
     //Each loop must perform EXACTLY the same set of commands/instructions
     long timeSpent = 0;
    
     //call for loop method
     timeSpent = forFindPrime();
     System.out.println("For loop spent " + timeSpent + " ms\n");
     
     //call do-while loop method
     timeSpent = doWhileFindPrime();
     System.out.println("For do-while loop spent " + timeSpent + " ms\n");
    
     //call while loop method
     timeSpent = whileFindPrime();
     System.out.println("While loop spent " + timeSpent + " ms");
    }
    
    public static long forFindPrime()
    {
     long startTime = getCurrentTimer(); //start timer
     for (int i = 1; i<=cycles; i++)
        {
         //loop through all the modulos
        for (int j = 2; j<=9; j++)
           {
            if (i==j)
             {
               j++; //skip... divisible by itself
             }
            if (i%j==0)
             {
    	isPrime = false;
    	break;
             }
            else 
             {
    	isPrime = true;
              }
          }
          if (isPrime == true)
           {
    	//System.out.println (i + " is prime");
    	counter++;
           }
       }
     long stopTime = getCurrentTimer();// end timer.. 
     System.out.println ("Number of prime elements found: " + counter);
     counter=0; //reset the counter
     return (stopTime-startTime);
    }
    
    public static long doWhileFindPrime()
    {
     long startTime = getCurrentTimer(); //start timer
     int i = 1;
     do{
         //loop through all the modulos
         for (int j = 2; j<=9; j++)
            {
    	if (i==j)
    	{
    	  j++; //skip... divisible by itself
    	}
    	if (i%j==0)
    	{
    	  isPrime = false;
    	  break;
    	}
    	else 
    	{
    	 isPrime = true;
    	}
    	}
    	if (isPrime == true)
    	{
    	 //System.out.println (i + " is prime");
    	 counter++;
    	}
    	i++;
    	}while(i<=cycles);
    		
        long stopTime = getCurrentTimer();// end timer.. 
        System.out.println ("Number of prime elements found: " + counter);
        counter=0; //reset the counter
        return (stopTime-startTime);
    }
    	
    	
    public static long whileFindPrime()
    {
      long startTime = getCurrentTimer(); //start timer
      int i = 1;
     while (i<=cycles)
           {
            //loop through all the modulos
           for (int j = 2; j<=9; j++)
              {
    	if (i==j)
    	{
    	  j++; //skip... divisible by itself
    	}
    	if (i%j==0)
    	{
    	 isPrime = false;
    	 break;
    	}
    	else 
    	{
    	isPrime = true;
    	}
             }
             if (isPrime == true)
    	{
    	 //System.out.println (i + " is prime");
    	 counter++;
    	}
    	 i++;
    	}
    		
        long stopTime = getCurrentTimer();// end timer.. 
        System.out.println ("Number of prime elements found: " + counter);
        counter=0; //reset the counter
        return (stopTime-startTime);
    }
    	
    public static long getCurrentTimer()
    	{
    		return System.currentTimeMillis();
    	}	
    }
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Sorry ... I didn't post any results:
    Number of prime elements found: 11428576
    For loop spent 4407 ms

    Number of prime elements found: 11428576
    For do-while loop spent 4593 ms

    Number of prime elements found: 11428576
    While loop spent 4375 ms
    But on another run the results where:
    Number of prime elements found: 11428576
    For loop spent 4375 ms

    Number of prime elements found: 11428576
    For do-while loop spent 4593 ms

    Number of prime elements found: 11428576
    While loop spent 4375 ms
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    That's really interesting. Here's 3 runs on my amd 1ghz anthon.
    Java Code:
    s:\temp>java LoopTimer
    Number of prime elements found: 11428576
    For loop spent 9684 ms
    
    Number of prime elements found: 11428576
    For do-while loop spent 9654 ms
    
    Number of prime elements found: 11428576
    While loop spent 9684 ms
    
    
    s:\temp>java LoopTimer
    Number of prime elements found: 11428576
    For loop spent 9854 ms
    
    Number of prime elements found: 11428576
    For do-while loop spent 9384 ms
    
    Number of prime elements found: 11428576
    While loop spent 9303 ms
    
    s:\temp>java LoopTimer
    Number of prime elements found: 11428576
    For loop spent 9454 ms
    
    Number of prime elements found: 11428576
    For do-while loop spent 9353 ms
    
    Number of prime elements found: 11428576
    While loop spent 9353 ms
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Thanks...

    Thanks for posting your results... and the results you obtained are very similar to mine. I'm going to do some more testing to see if I can get anything different, but for a 50 millon cycle calculation, for the most part, the difference in performance between the different loop methods is minmal and can be dismissed as insignificant.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM
  2. Applet performance problem...
    By pmrenaud in forum Java Applets
    Replies: 0
    Last Post: 01-07-2009, 03:47 PM
  3. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 10:24 PM
  4. performance problem on osx
    By coldnebo in forum Advanced Java
    Replies: 3
    Last Post: 08-01-2008, 09:39 PM
  5. Timing array loop performance
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •