Thread: Simplification of nested loops in C

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    8

    Simplification of nested loops in C

    Hello,
    I have a program where I must use nested loops. I want to make K loops with K and a parameter, the size of each loop in an array N [K], a table of indices i [k]:

    tableau des indices i[k] :
    Code:
    for ( i[0]=0 ; i[0]<N[0] ;  i[0]++){
          for ( i[1]=0 ; i[1]<N[1] ; i[1]++){
                for ( i[2]=0 ; i[2]<N[2] ;  i[2]++){
                       for ( i[3]=0 ; i[3]<N[3] ; i[3]++){
                                             .
                                             .                  
                                             .
                                             for ( i[K-1]=0 ; i[K-1]<N[K-1] ; i[K-1]++){
                                                          ..........................
                                             }
                                             .
                                             .                  
                                             .
                      }
                }
         }
    }
    
    
    Each time the K increases the program become heavy and I can not seem to execute it.
    How I can reduce the loops in an optimal way to speed up my program.


    Thank you for your help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This would allow you to choose a K value without having to edit the code each time.
    Code:
        i[0]= i[1] = i[2] = 0;
        do {
          int x;
          printf("%d %d %d\n", i[0], i[1], i[2] ); // for example, if K = 3
          for ( x = K-1 ; x >= 0 ; x-- ) {
            if ( ++i[x] == N[x] ) {
              if ( x > 0 ) i[x] = 0;
            } else {
              break;
            }
          }
        } while ( i[0]<N[0] );
    > How I can reduce the loops in an optimal way to speed up my program.
    You need to tell us what you're trying to do if you want us to suggest a better way than try every possible combination.

    Rewriting the code as above won't make it any quicker. All the loops are still there, they're just less obvious.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    Thank you.

    Suppose that I have a matrix M of dimension K and I want to calculate the partial derivation for each dimension of this matrix.
    Then for dimension K I have to use K loops for each partial derivative




    If K = 3:


    Code:
    for(i2=0 ; i2<N[2] ; i2++){
        for(i1=0 ; i1<N[1] ; i1++){
            for(i0=1 ; i0<N[0] ; i0++){
                Dx0[i0-1][i1][i2] = M[i0][i1][i2] -M[i0-1][i1][i2]  ;
            }
        }
    }

    Code:
    for(i0=0 ; i0<N[0] ; i0++){
        for(i2=0 ; i2<N[2] ; i2++){
            for(i1=1 ; i1<N[1] ; i1++){
                Dx1[i0][i1-1][i2] = M[i0][i1][i2] -M[i0][i1-1][i2]  ;
            }
        }
    }
    Code:
    for(i1=0 ; i1<N[1] ; i1++){
        for(i0=0 ; i0<N[0] ; i0++){
            for(i2=1 ; i2<N[2] ; i2++){
                Dx2[i0][i1][i2-1] = M[i0][i1][i2] -M[i0][i1][i2-1]  ;
            }
        }
    }
    I'm looking for a general method to decrease the amount of code.
    Last edited by benhilal; 11-27-2016 at 08:38 AM.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    No such general method exists.. though perhaps in some specific cases the code could be reduced, unfortunately it is up to you to decide your specific cases and if some optimization can be applied here.. but don't expect anyone to see it. It's up to you.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I suppose I could suggest you use a code-generator to automatically generate the large amount of nested loops and nasty looking matrix indexing? Best I got...

    Basically, instead of writing all that nasty stuff, write a program that would generate all that, given whatever parameters there are.. like matrix dimensions, whatever... sort of like a web page template system... then compile the output of that. It will still be ugly but at least you won't have to deal with writing it by hand!

    Also, if there are any optimizations your program could make, do it. Then you're on you're way to writing a real type of compiler. First get it working correctly of course...
    Last edited by MacNilly; 11-28-2016 at 11:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested Loops Help.
    By etricity in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2014, 03:32 AM
  2. nested for loops
    By bknick24 in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2014, 07:02 PM
  3. Help with nested loops?? Please help!
    By GoBlue13 in forum C++ Programming
    Replies: 12
    Last Post: 02-28-2013, 07:27 AM
  4. Nested loops..
    By her091 in forum C Programming
    Replies: 12
    Last Post: 06-17-2012, 02:22 PM
  5. Help nested for loops
    By dals2002 in forum C Programming
    Replies: 14
    Last Post: 03-14-2008, 01:18 AM

Tags for this Thread