I have compared the times required by the Arduino sqrt(N) function and the corresponding function isqrt32(N) used in the BotBoarduino_CH3R_PS2.ino code. I found the Arduino function to be more than 4 times faster and significantly more accurate. My code computes the square root of 5500 100000 times for each method. After uploading it there is a 5 second delay to let you enable the serial monitor. Here is my code:
[code]unsigned long time;
unsigned long start_time;
unsigned long end_time;
unsigned long dt;
unsigned long N = 5500;
unsigned long cycles = 100000;
double result;
// Square root function from BoardArduino code
unsigned long isqrt32 (unsigned long n) //
{
unsigned long root;
unsigned long remainder;
unsigned long place;
root = 0;
remainder = n;
place = 0x40000000; // OR place = 0x4000; OR place = 0x40; - respectively
while (place > remainder)
place = place >> 2;
while (place)
{
if (remainder >= root + place)
{
remainder = remainder - root - place;
root = root + (place << 1);
}
root = root >> 1;
place = place >> 2;
}
return root;
}
void setup() {
Serial.begin(9600); // open the serial port at 9600 bps
delay(5000); // Time available to actuate serial monitor
}
void loop() {
Serial.print("Compare times to compute square root of ");
Serial.println(N, DEC);
start_time = millis();
for (double i = 0; i < 100000; i++)
{
result = sqrt(N);
}
end_time = millis();
dt = end_time - start_time;
Serial.print("Arduino result = ");
Serial.println(result, DEC);
Serial.print("Arduino time = ");
Serial.println(dt, DEC);
start_time = millis();
for (double i = 0; i < 100000; i++)
{
result = isqrt32(N);
}
end_time = millis();
dt = end_time - start_time;
Serial.print("LynxMotion result = ");
Serial.println(result, DEC);
Serial.print("LynzMotion time = ");
Serial.println(dt, DEC);
Serial.println();
}
[/code]
Result:
Compare times to compute square root of 5500
Arduino result = 74.1619873046
Arduino time = 1188
LynxMotion result = 74.0000000000
LynzMotion time = 4975
It looks like it would be better to use the Arduino function.
Ted