Thread: C codes takes more timing cycles_Better OPTIMIZATION for Codes

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    52

    C codes takes more timing cycles_Better OPTIMIZATION for Codes

    Hi,

    It takes more timing cycles.

    Starts at 46 clock cycles but execution continues for 678 clock cycles. Trying to reduce execution time. Please modify codes.

    Code:
     #include <stdio.h>
    
        int main()
       {
        int n,r,c,no_e,S;
        int y[6] = {0};
    
        int i,j,pos,I;
        int Imax = 20;
        int E[4][6] = {0};
        int H[4][6] = {1,1,0,1,0,0,0,1,1,0,1,0,1,0,0,0,1,1,0,0,1,1,0,1};
        int M[6] = {0,0,1,-1,-1,-1};
        n = 6;
        r = 4;
        c = 6;
    //Decoding using Message passing in Erasure channel
        no_e = 0;
        S = 0;
        pos = 0;
        I = 0;
        for(j=0;j<r && I<Imax;j++)
        {
            for(i=0;i<n;i++)
            {
                if(H[j][i] == 1)
                {
                    if(M[i] < 0)
                    {
                        no_e = no_e + 1;
                        pos = i;        //The position of erasure bit
                    }
                        if(M[i] == 0 || M[i] == 1)
                        S = S + M[i];
                }
            }
                if(no_e > 1)
                 for(i=0;i<n;i++)
                    E[I][i] = -1;       //Make the entire jth row -1
            else
                for(i=0;i<n;i++)
                    E[I][i] = M[i];
            if(no_e == 1)
            {
                E[I][pos] = S%2;      // Set the bit with the correct value
                M[pos] = S%2;         //Replace the bit with the corrected  
            }
    
            no_e = 0;
            S = 0;
    
            for(i=0;i<n;i++)
                if(M[i]<0)
                    no_e = no_e+1;  
            if(no_e == 0)
                break;
    
            I = I + 1;
            no_e = 0;
            if(j==r-1)
                j=0;   
         }
    
         printf("\n\n\nThe corrected Code Word is(%d): \n\n",I);
         for(i=0;i<n;i++)
            printf("%d\t",M[i]);
        }
    C codes takes more timing cycles_Better OPTIMIZATION for Codes-latency-png

  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
    Is that with or without the printf statements at the end?

    Also, does that time include the time it takes the OS to load your program, and then for the C runtime system to initialise itself before it even gets to calling main?

    Also, does that time include any time spent inside interrupt service routines which may happen while your program is running?

    Also, did you make use of any compiler optimisation flags?

    How about compiling it with more warnings?
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:11:17: warning: missing braces around initializer [-Wmissing-braces]
       int H[4][6] = { 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1 };
                     ^
    foo.c:11:17: note: (near initialization for ‘H’)
    foo.c:10:7: warning: variable ‘E’ set but not used [-Wunused-but-set-variable]
       int E[4][6] = { 0 };
           ^
    foo.c:6:7: warning: unused variable ‘y’ [-Wunused-variable]
       int y[6] = { 0 };
           ^
    foo.c:5:13: warning: variable ‘c’ set but not used [-Wunused-but-set-variable]
       int n, r, c, no_e, S;
                 ^
    That E thing is a killer for you.
    There's a whole bunch of code dedicated to setting E which serves no purpose whatsoever.

    Code:
    #include <stdio.h>
    
    int main()
    {
      int n, r, no_e, S;
      //!!int y[6] = { 0 };
    
      int i, j, pos, I;
      int Imax = 20;
      //!!int E[4][6] = { 0 };
      int H[4][6] = {
        { 1, 1, 0, 1, 0, 0 },
        { 0, 1, 1, 0, 1, 0 },
        { 1, 0, 0, 0, 1, 1 },
        { 0, 0, 1, 1, 0, 1 },
      };
      int M[6] = { 0, 0, 1, -1, -1, -1 };
      n = 6;
      r = 4;
      //!! c = 6;
    //Decoding using Message passing in Erasure channel
      no_e = 0;
      S = 0;
      pos = 0;
      I = 0;
      for (j = 0; j < r && I < Imax; j++) {
        for (i = 0; i < n; i++) {
          if (H[j][i] == 1) {
            if (M[i] < 0) {
              no_e = no_e + 1;
              pos = i;              //The position of erasure bit
            }
            if (M[i] == 0 || M[i] == 1)
              S = S + M[i];
          }
        }
    //!!     if (no_e > 1)
    //!!       for (i = 0; i < n; i++)
    //!!         E[I][i] = -1;           //Make the entire jth row -1
    //!!     else
    //!!       for (i = 0; i < n; i++)
    //!!         E[I][i] = M[i];
        if (no_e == 1) {
    //!!       E[I][pos] = S % 2;        // Set the bit with the correct value
          M[pos] = S % 2;           //Replace the bit with the corrected
        }
    
        no_e = 0;
        S = 0;
    
        for (i = 0; i < n; i++)
          if (M[i] < 0)
            no_e = no_e + 1;
        if (no_e == 0)
          break;
    
        I = I + 1;
        no_e = 0;
        if (j == r - 1)
          j = 0;
      }
    
      printf("\n\n\nThe corrected Code Word is(%d): \n\n", I);
      for (i = 0; i < n; i++)
        printf("%d\t", M[i]);
      printf("\n");
    }


    And still, you fail to provide details of your ALGORITHM. The single useful comment in your code suggests you're up to something to do with error detection and recovery.
    Erasure channel - Wikipedia



    It's like if you implemented bubble sort, then the best we could come up with is a slightly better bubble sort. But the real answer would be to throw the code away and implement quicksort.
    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
    Mar 2017
    Posts
    52
    Thanks for your response. Working with message passing. For ALGORITHM click [link] []SPM: 404 Page Not Found.

    -1 indicates the missed/Erasure bit. Then how to executes the codes. Please help and guide me.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    "codes"?

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Quote Originally Posted by Hodor View Post
    "codes"?
    What you telling ?

  6. #6
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Hi,

    If you see the attachment, first loop takes more time. Any optimization method ? welcome

    Code:
        for(j=0;j<ROWS && I<Imax;j++)
        {
    
            for(i=0;i<COLS;i++)
    
            {
    
                if(matrix[j][i] == 1)
                {
    
                    if(M[i] < 0)
                    {
    
                        no_e = no_e + 1;
                        pos = i;        //The position of erasure bit
                    }
                    if(M[i] == 0 || M[i] == 1)
                        S = S + M[i];
                }
            }
            if(no_e > 1)
                for(i=0;i<n;i++)
    
    
                    E[I][i] = -1;       //Make the entire jth row -1
            else
                for(i=0;i<n;i++)
    
                    E[I][i] = M[i];
            if(no_e == 1)
            {
    
                E[I][pos] = S%2;      // Set the bit with the correct value
                M[pos] = S%2;         //Replace the bit with the corrected bit before further proceeding
            }
    
            for(i=0;i<n;i++)
    
    
                if(M[i]<0)
                    no_e = no_e+1;     // Calculating no of errors in entire message
    
            if(no_e == 0)
                break;
    
            I = I + 1;
            no_e = 0;
            if(j==ROWS-1)
                j=0;                   
        }
    C codes takes more timing cycles_Better OPTIMIZATION for Codes-loop-png

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you haven't addressed what the E array is for.
    It's still unused as far as I'm concerned.

    > M[pos] = S%2;
    On the off chance that your compiler is too stupid to replace this with
    M[pos] = S & 1;
    perhaps you could do it yourself.

    Are you turning optimisations on before doing timing tests?


    > if(M[i] == 0 || M[i] == 1)
    Having already established that M[i] isn't less than zero, perhaps
    else if(M[i] <= 1)
    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.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by SarasMuthu View Post
    What you telling ?
    Hodor was tittering at the dearth of linguistic perspicacity you exhibit vis a vis English.

    Anyway, it seems unlikely to me that your code is even correct. You should work on that first. Remember to maximize your warning level and fix all warnings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validate area codes within a list of area codes
    By Staja24 in forum C Programming
    Replies: 9
    Last Post: 05-06-2015, 09:28 PM
  2. help me with C codes
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 03-21-2009, 12:52 AM
  3. c codes need help,thanks
    By hth373737 in forum C Programming
    Replies: 21
    Last Post: 11-23-2008, 10:45 PM
  4. VK codes
    By Hunter2 in forum Windows Programming
    Replies: 10
    Last Post: 08-24-2002, 09:58 AM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM

Tags for this Thread