Code module tip: faster code when avoiding arrays and loops
Archive · 20063 old posts
From the original SynthMaker forum (2004–2014), with its members’ names and dates.
rl ·
when trying to make some of my code more efficient I made the interesting observation that sometimes it's far more faster not to use arrays and loops but use good old linear spagetti code instead. I could measure up to 8 times less clock cycles when avoiding loops and arrays:
As an example have a look at this nice and clean code:
float i;
float cos[16], sin[16];
...
i=0;
loop (16){
cos[i]=1; sin[i]=0;
i=i+1;
}
as an example the cos[i]=1; assignment is translated into:
The compiled assembly is (in this example) not only smaller but also using SSE code and executes much faster. If there're more instructions within the loop this might change, so always check your code for improvements.
sambean ·
It can be pretty ugly when using arrays in the code.. Note: you've also got to convert the 'i' from float to integer... This method though wouldn't be much use with a variable array length though..
Just thinking out loud here: what the lengthy asm code is doing, is basically sorting out the interleving for the arrays, which have to store all the 4 SSE channels in a single array.. If we had an integer 'index' SSE data type in SM code, which basically had the interleving offset built into it at initialization, and you could only add/subtract to it and there were special operations for looping, the use of arrays in SM could be a lot faster..
Sam
doug4350 ·
Hi sambean,
You may already know this, but for the rest of the gang, what you've done is what's known to optimizing compilers as "unrolling a loop". By repeating the code within the loop the # of iterations of the loop eliminates the overhead of the loop itself. AFAIK, it can only be done if the # of iterations is constant. Also, the size of the code increases linearly for each line of code within the loop.
Just be careful when cutting/pasting the iterations. It's very easy to induce a bug like this (especially after staring at code for 10+ hours):