how long sin() function takes?

Hi, I have a problem with sin() function. I want to check the how long a sin() function take, and the following codes give a big difference in the result.

  y = 10 + 5*sin(2*PI*0.04*250 + 70*PI/180);

This one, it takes about 5us for executing. While

  t = 250; 
  y = 10 + 5*sin(2*PI*0.04*t+ 70*PI/180);

This one, it takes about 55us for executing. So if I use a variable inside the sin() function, it will take much more time for executing than if I use a direct number in the sin() function.

Does any one know why? And is there any way to solve this problem ( I mean how to use a variable inside the sin() function without increasing the time it takes for executing) .

Thanks a lot.

Does any one know why? And how to

for that you need to dig very deep into the compiler and its optimization functions.
Those timings will depend sometimes also from compiler switches!

While in your first example the right expression is just a compile time constant, the compiler may do the full (or partial) calculation already at compile time.
So you measure perhaps only the assignment of a constant value to a variable.

In the examples from your second post the code, you are using is missing. There might be a difference if you are using just constants in the sin() function, or variables, where you assigned values before.

The sin() function itself has a different runtime behavior as well. Most implementations are working with lookup tables and interpolation. Than it depends, how close the values to a given entry in the tables are.

Have a look at:

./hardware/arduino/sam/system/CMSIS/CMSIS/DSP_Lib/Source/FastMathFunctions/arm_sin_f32.c

Good question. Short answer , that complier optimized away sin at compiling time, substituting pre-calculated result into code.
If you are using a variable, sin function works as expected, to be sure you may declare variable as volatile.
I try to find more definitive answer, but interesting things, my googling for "benchmark cortex-M3 / ARM / SAM3 " turns blank. There is no precise timing, and only approximation:

same core M3, about 20% less cpu clock. So a lot depends on compiler, options/ settings / environment passed to compiler, version etc.
You may measure timing in the "for" loop, 1000 - 10000 calculation to get better precision, with volatile declaration.
To have better than 10 usec, use LUT, likely there is a big flash memory size.

Thanks @Magician.
I declared the variable as volatile , but it didn't become better.

I declared the variable as volatile , but it didn't become better.

Right, it shouldn't. Declaration volatile keeps compiler away from doing it's tricks, when you get 5 usec results - wrong results.

Declaring the variable as volatile only prevents the compiler from optimization and assuming the content of the variable as a constant value. With that you are only forcing, that no optimization is made, not calling sin() at all.

Look at the source of the sin() I posted above! That is the function, which is called on the DUE!

Thanks @Kassiopeia

I will check link.
Thanks

If you do not need exact values you could check my investigation in a faster sin() here

-http://forum.arduino.cc/index.php?topic=69723.0 -

not tried myself but should work on a DUE too (please confirm)

and even faster integer version - http://forum.arduino.cc//index.php?topic=196625.msg1453433#msg1453433 -
but of course less precise