Fletcher_Chr:
Hi
For raw cos(x) I get around 8900 loops/sec preformance.
Code:
float a=0.5;
long bigNumber = 100000;
unsigned long time1, time2, diff;
void setup(){
Serial.begin(9600);
time1 = millis();
for (int i=0; i<bigNumber;i++){
a = cos(a);
}
time2 = millis();
diff = time2-time1;
Serial.print(diff);Serial.print(" millisec for numbers of loops: ");Serial.println(bigNumber);
a = 1.0bigNumber/diff1000;
Serial.print(a); Serial.println(" loops/sec");
}
void loop(){
}
Output:
11229 millisec for numbers of loops: 100000
8905.51 loops/sec
I guess there is a convagens to a=0.739... but the cos() calculation is still forced at every loop.
-Fletcher
Years ago this post comes back to life, but, I don't trust compilers, mostly when the 'a = cos(a)' becomes repetitive and the same number [a == cos(a)] = 0.739084823, after only 33 loops. The compiler CAN observe certain possibilities and include a few cache for repetitive operations, mostly considering a==cos(a) as 0.739084823. it could be an expected testing situation, so the compiler takes advantage of that and make 8+ thousand operations (without doing it) per second. You never know, only by observing the disassembled code.
To make sure about this your measurement, I would change the testing routine to make
for (int i=0; i<bigNumber;i++){
a = cos(a);
a = sin(a);
}
and then double bignumber in the accounts, just to make sure the compiler is not playing tricks.
Anyway, I just finished a sin(x) routine in assembly, ~210 AVR instructions, it calculates sin(x) based on the input angles in degrees, two bytes, from zero to 900 (90.0°), the result is ±1 bit of error in 3 bytes, so 23 bits of precision along all the first quadrant angles. The AVR running a 16MHz can do it in 211us for 0.01° to 213us for 89.9°, so it can do a bit more than 4700 calculations per second. It is based on Taylor series, so there is a lot of calculations, no floating point, and it runs all in the registers, no data segment used.
Of course CORDIAC would be faster, and perhaps your test was done in a compiler using CORDIAC, built in assembly code for speed.