speeding up nested for loop

This is a discussion on speeding up nested for loop within the C++ Programming forums, part of the General Programming Boards category; ok, I have 3 nested for loops and I want to start at a particular point in the loop. what ...

  1. #1
    Unregistered
    Guest

    speeding up nested for loop

    ok, I have 3 nested for loops and I want to start at a particular point in the loop. what recommendations can you give me?

    here is a sample code:
    Code:
    int main()
    {
     int a,b,c,d;
     for(a=0;a<10;a++)
      for(b=0;b<10;b++)
       for(c=0;c<10;c++)
        for(d=0;d<10;d++)
         {
          if((a==0)&&(b==0)&&(c==0)&&(d==0))
          {
           a=1,b=2,c=3,d=4;
          }
          cout<a<<b<<c<<d;
         }
    }
    in my sample code, I have 3 nested for loops and it prints from "1234" to "9999" because I added an if statement to change some values. however, the if statement would slow down the nested for loops because it would be checked every time during a loop. any suggestion on another solution?

    can the use of static work for this situation? if so, how?

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    what if you nested the if in another if that skiped that later after d clicked for however many number of times?

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    for (a=1;a <=9; ++a)
    for (b=2;b <=9;++b)

    etc...
    Blue

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>what if you nested the if in another if that skiped that later after d clicked for however many number of times?



    That would still require one if statement to be evaluated.
    Blue

  5. #5
    Unregistered
    Guest
    betazep, I think your solution is wrong.

    the "for(b=2;b<=9;... " will make b start from 2 and ignore 1.

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Grr I hate multiple unreggeds in the same thread, makes it hard to tell if they are or are not the same entity.

    I THINK that the first unregged wanted a program in which b started at 2, c at 3, and d at 4.

    BUT I think blue's solution would mess up at 2000. It would skip straight to 2234.

    A more effective method might be to place the choice in the declarator for the loops as:

    Code:
               
       int main() {
          int a,b,c,d;
          for(a=1;a<10;++a) {
             for(b=a==1?2:0;b<10;b++) {
                for(c=a==1?3:0;c<10;c++) {
                   for(d=a==1?4:0;d<10;d++) {
                      cout<<a<<b<<c<<d;    
                   }
                }
             }
          }
       }
    This makes the test occur only once every tenth, twice every hundredth, and thrice every thousandth iteration, rather than every single iteration.

    Of course, this assumes you want every number from 1234 - 9999. Your original program has the same logic error/unusual usage as beta's solution. They both give 1234 - 1999, 2234 - 2999, 3234 - 3999, etc.

    This is, of course, assuming that

    Code:
    for (int n = 1234; n<10000; ++n) {
    cout << n;
    }
    would be too simple.
    Last edited by Imperito; 03-16-2002 at 04:34 PM.

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    If 1234->9999 is required something like this should work -

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
     int a,b,c,d;
     for(a=1;a<10;a++)
    	 for(a==1?b=2:b=0;b<10;b++)
    		 for(a==1&&b==2?c=3:c=0;c<10;c++)
    			 for(a==1&&b==2&&c==3?d=4:d=0;d<10;d++)
    			{		
    				cout<<a<<b<<c<<d<<endl;
    			 }
    }

  8. #8
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>betazep, I think your solution is wrong.

    the "for(b=2;b<=9;... " will make b start from 2 and ignore 1.<<<

    good catch.
    Blue

  9. #9
    Unregistered
    Guest
    Sorensen, the code you gave would cost more time because the "?" will compare values for every pass in the nested loop.

    the if statement from the first post is likely to finish the nested loop at a faster time. I also thought of something. can this possibly work?

    Code:
    int main()
    {
     int a,b,c,d;
     for(a=0, static a=1;  a<10;  a++)
      for(b=0, static b=2;  b<10;  b++)
       for(c=0, static c=3;  c<10;  c++)
        for(d=0; static d=4; d<10;  d++)
          cout<a<<b<<c<<d;
    }

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Sorensen, the code you gave would cost more time because the "?" will compare values for every pass in the nested loop.

    No it won't. Do variables get initialised every pass of the for loop?

  11. #11
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Ach soren beat me to it. But my post of the solutionis higher up and better documented so

  12. #12
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Yeah unregged, like I said in MY post

    This makes the test occur only once every tenth, twice every hundredth, and thrice every thousandth iteration, rather than every single iteration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 09:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21