Thread: techniques for optimising a for loop..

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    techniques for optimising a for loop..

    so, I've got this for loop which does a really simple thing:

    Code:
    for(i = 0; i < 4096; i++){
    
       int sineindex = i % 16;
    
       total += soundsamples[i] * sinetable[sineindex];
    
    }
    the question is.. is it possible, at all, to optimise this further? Or do I have to go to assembly? just trying to analyse sine waves.. real quick.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Leave it up to your compiler. I suppose you could try to unroll your loop with 16 assignments in a single pass, but again, most compilers will optimize better than you can.


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

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You're taking a dot product with a sine wave. If you do that at a series of different time shifts, that's a convolution. A convolution can be optimized with an FFT, yielding O(n/log n) speedup.

    More information on what you are attempting would help me suggest further optimizations. What you have here, for its obvious purpose, isn't too bad, assuming the compiler optimizes that modulus as it should be doing.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Hmm, looks like there's not much I can do then.. I'm only doing this for one series at a time. I am using FFT (in particular kiss fft) but for an unrelated set of operations..

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ time saving techniques
    By gvkalra in forum C++ Programming
    Replies: 6
    Last Post: 09-16-2008, 01:02 AM
  2. Our First Article - Searching Techniques By Prelude
    By kermi3 in forum Article Discussions
    Replies: 17
    Last Post: 01-31-2005, 05:48 PM
  3. general programming techniques e-books
    By kariem2k in forum C++ Programming
    Replies: 1
    Last Post: 12-30-2002, 04:09 PM
  4. Best techniques to learn C
    By swiftfire in forum C Programming
    Replies: 1
    Last Post: 12-04-2002, 12:16 PM
  5. Relaxation techniques
    By RobS in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 05-10-2002, 10:35 PM