Thread: Optimizing my code

  1. #16
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Just wondering if something like this would be faster....

    Code:
    ...
    
    
    for(i=0;j=0;i<M||j<n;i++;j++){
    
                                        coeffs[i][j] = xpwr * ypwr;
                                        ypwr *= y;
                                        sol += C[i][j] * coeffs[i][j];
                                }
    
    
    return sol;
    warning it may be a bad idea. it may not run faster.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kryptkat
    Just wondering if something like this would be faster....

    Code:
    ...
    
    
    for(i=0;j=0;i<M||j<n;i++;j++){
    warning it may be a bad idea. it may not run faster.
    Please explain that for loop to me.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    sure. i was asking if taking the four for() loops in the previous example and combining them in to two loops using one for() statement would be any faster. it was just an example to get the point across.... the example will not work as is the actual modifications to get it to work you or the op would have to do for themself. for(var1loop1;var2loop2;compairstatment or statements;loopinc;loopinc){statements};

    i know i messed up the forstatement logic but it was just an example and would not work as is. did it quik with out thinking.

    also is there a proper name for the double loop for() ? messed up look to it but it is more compact.
    Last edited by kryptkat; 10-22-2006 at 05:54 AM.

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    @Lina
    It would be really useful if you could provide a main() which sets up some data, calls that function and prints the expected results.

    And it would be useful to know how you calculate ACPE for a given function.

    It's a lot easier to see whether an optimisation works if we have a working frame of reference to compare against.
    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.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kryptkat
    did it quik with out thinking.
    Yes, that was definitely apparent.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    Quote:
    Originally Posted by kryptkat
    did it quik with out thinking.
    Yes, that was definitely apparent.
    queston still stands .... would it be faster to use one for() with multi var?

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    18
    Hi,

    whenever i type
    Code:
    static val_t coeffs[M][N] = {0};
    i get an error tha says=
    missing braces around initializer
    (near initialization for `coffs[0]')
    why is that?

  8. #23
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Lina
    Hi,

    whenever i type
    Code:
    static val_t coeffs[M][N] = {0};
    i get an error tha says=
    missing braces around initializer
    (near initialization for `coffs[0]')
    why is that?
    For just the reason it says. It is saying that it would prefer full braces, but I chose not to. It does what I expect, and the warning is telling me something I am aware of, but choose to ignore.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    18
    can you show how you would do it coz i tried to do it and i didn't work

  10. #25
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Is it an error or is it a warning? I get the warning, understand it, ignore it; the code builds, I run it.

    To do the full bracing, you need to do this
    Code:
    static val_t coeffs[M][N] = {{0,0,0},{0,0,0},{0,0,0}};
    for whatever dimensions M and N are. Or do the same for any M and N using {0}.

    [edit]Attached my test code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    18
    when i tested the following, it gave me wrong results.

    Code:
    val_t poly2d_dave(val_t C[M][N])
    {
       static val_t coeffs[M][N] = {0};
       static int init = 0;
       int i, j;
       val_t sol = 0.0;
    
       if ( init == 0 )
       {
          val_t xpwr = 1.0;
          val_t ypwr = 1.0;
          val_t x = *x_p;
          val_t y = *y_p;
          for ( i = 0; i < M; i++ )
          {
             ypwr = 1.0;
             for ( j = 0; j < N; j++ )
             {
                coeffs[i][j] = xpwr * ypwr;
                ypwr *= y;
             }
             xpwr *= x;
          }
          init = 1;
       }
    
       for ( i = 0; i < M; i++ )
       {
          for ( j = 0; j < N; j++ )
          {
             sol += C[i][j] * coeffs[i][j];
          }
       }
       return sol;
    }

  12. #27
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > when i tested the following, it gave me wrong results.
    Which is why YOU need to post a complete compilable program which we can just load and run without having to do any guessing as to what input data to pass, or what output to expect.
    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.

  13. #28
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you really want help, let us all play on the same field. Show your actual code, please.
    Quote Originally Posted by Salem
    @Lina
    It would be really useful if you could provide a main() which sets up some data, calls that function and prints the expected results.

    And it would be useful to know how you calculate ACPE for a given function.


    It's a lot easier to see whether an optimisation works if we have a working frame of reference to compare against.
    Quote Originally Posted by Dave_Sinkula
    I am not familiar with ACPE, but I've tried to look into it a bit. I was wondering if you've got some tool that you use that might be helpful to share with the rest of us?

    Also, such as in the case of your precomputed version, is the amortization over a single call to the function, or is it amortized over all calls?
    Quote Originally Posted by Dave_Sinkula
    Is it an error or is it a warning?
    Last edited by Dave_Sinkula; 10-22-2006 at 12:18 PM. Reason: D'oh! Pokey again. :(
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    18
    M=83, N=97, system clock rate=3190 MHz

    Error in poly2d_dave
    x=1.062500 y=-0.937500 Output = 3538.000000 but should be 44581.437500

  15. #30
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Now if only I had C, i might have a prayer at reproducing this.
    Code:
    val_t C[M][N] = {{1.2,3.4,5.6},{7.8,9.0,-2.3},{-4.5,-6.7,-8.9}};
    val_t X = 1.062500, *x_p = &X;
    val_t Y = -0.937500, *y_p = &Y;
    
    int main(void)
    {
       long count = 10000;
       test(count, BIND(poly2d_base),    C);
       test(count, BIND(poly2d_precomp), C);
       test(count, BIND(poly2d_dave),    C);
       test(count, BIND(poly2d_horner),  C);
       return 0;
    }
    
    /* my output
    poly2d_base     = -6.71054, 10000 iterations : 2.493
    poly2d_precomp  = -6.71054, 10000 iterations : 2.294
    poly2d_dave     = -6.71054, 10000 iterations : 2.042
    poly2d_horner   = -6.71054, 10000 iterations : 2.114
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. optimizing my quaternion code
    By confuted in forum Game Programming
    Replies: 9
    Last Post: 08-16-2003, 08:50 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM