# Real time execution cost of sin/cos function

Hi to all,

i’m new to the forum (and arduino :)) i’m trying to create a library that use Look Up Table to calculate sin/cost faster (with loss precision, of course). I’ve seen that this is a common topic but, my problem is to calculate the real time that sin/cos standard consume, and i get strange result.

This is my code where i simply initialize an array with 100 value and make a 1000 * 100 calculation (just to consume less memory and make more calc).

If multiplier is 100, 1000 or 10000 i got even the same result:

Std: 0.0000040000

I really can’t understand why…

In this way, i cant test if my sin/cos that use LUT, will be faster or not… where i wrong ?

Thanks a lot for your patience!

``````#define SIN_ARR_SIZE 100
#define MULTIPLIER 10000

float testSinStd() {
float tmp = 0.0;
unsigned long startTime, endTime;

startTime = micros();
for (int j = 0; j < MULTIPLIER; j++) {
for (int i = 0; i < SIN_ARR_SIZE; i++) {
sin(sin_seeds[i]);
}
}
endTime = micros();

return (endTime - startTime) / 1000000.0f;
}

void setup() {
Serial.begin(9600);

// Populate sin value
for (int i = 0; i < SIN_ARR_SIZE; i++) {
sin_seeds[i] = random(0, 4* PI);
}

Serial.print("Std: ");
Serial.println(testSinStd(), 10);

}
``````

can’t seem to find any obvious error in your code. Have you tried not dividing by 1000000.0f to see how many micros it is returning? try also reducing multiplier to 1 or 10. Finally, replace micro() by millis() and see if it gives you another reading.

Removing the division by 1000000.0f result is : Std: 4.0000000000

Changing multiplier to 1 or 10 result is: Std: 4.0000000000

Changing micros in millis (and trying with multiplier 1, 10 or 1000) result is even: Std: 0.0000000000

Maybe the compiler sees that you’re not using the result and optimizes the sin call to nada.

Trying to "confuse" the optimizer, i've tried to assign the sin result to a variable and next add to result 1 (just to use at least one time the variable), but nothing change..

The compiler has decided that you aren't really doing anything as there is no output, other than the elapsed time calculation. Try adding a loop at the end, after you print out the elapsed time, that prints out the calculated values, or at least some subset of them.

It shouldn't depend on output. Manipulating variables in memory space is a plausible task. Try replacing the seed with the sin of itself, in the array.

jboyton: The compiler has decided that you aren't really doing anything as there is no output, other than the elapsed time calculation. Try adding a loop at the end, after you print out the elapsed time, that prints out the calculated values, or at least some subset of them.

Using this tecnique i think to have resolved.

I've added a variable that is the total result and in every execution i add the sin result to this variable and, after the loop, i output the value.

in this way, i get this:

Std: 12215776.0000000000

That is the correct value of the calculation time (i think).

Yes, but you have the overhead of the floating point addition that way. Also, there is a way to disable optimization, but I am not sure how.

aarg: It shouldn't depend on output. Manipulating variables in memory space is a plausible task. Try replacing the seed with the sin of itself, in the array.

In my experience there has to be some output. It doesn't have to be to the serial monitor but to some tangible hardware other than internal memory. I'd love to see a counter example.

Yes its right that i have the sum opertion that consume CPU Time, but in this way i can compare my function (sin standard and sin with LUT).

But however, i will do the same sum operation in both, so the difference is equiparable.

mistre83: Yes its right that i have the sum opertion that consume CPU Time, but in this way i can compare my function (sin standard and sin with LUT).

But however, i will do the same sum operation in both, so the difference is equiparable.

Really? To make a valid comparison, don't you have to be spending almost all your time calculating the sine? It would be like the difference between t and (t+tn) where tn represents the addition time.

in this way, i get this:

Std: 12215776.0000000000

It's nonsense printing a float that has 6 or 7 digits of precision, to 10 decimal places.

Please check my faster sin() investigation here - http://forum.arduino.cc/index.php?topic=69723.0 -