not cut into half but ~30% off the time 8) [ IDE 1.5.8, UNO ]
// Test sketch for Arduino forum.
// http://forum.arduino.cc/index.php?topic=342651.0
// With Arduino 1.6.5
// Using an Arduino Uno board (no double, only float)
// No need to declare PI, that is already somewhere in the headers.
// To predefine a parameter, this prototyping is needed.
// But I don't understand the pointer to the result anyway.
// double distance_WGS84(double & latt_A, double & long_A, double & latt_B, double & long_B, double *distance = NULL);
void setup()
{
Serial.begin(9600);
Serial.println("\nDuration of floating point calculations");
}
void loop()
{
double latA, longA, latB, longB;
latA = random(0, 500);
longA = random(0, 500);
latB = random(0, 500);
longB = random(0, 500);
uint32_t startTime = micros();
double result = distance_WGS84(latA, longA, latB, longB);
uint32_t stopTime = micros();
Serial.print("Result : ");
Serial.print(result);
Serial.print(" That took : ");
Serial.print(stopTime - startTime);
Serial.print(" us");
Serial.println();
startTime = micros();
result = distance_WGS84_2(latA, longA, latB, longB);
stopTime = micros();
Serial.print("Result : ");
Serial.print(result);
Serial.print(" That took : ");
Serial.print(stopTime - startTime);
Serial.print(" us");
Serial.println();
delay(5000);
}
double distance_WGS84(const double & latt_A, const double & long_A, const double & latt_B, const double & long_B)
{
//Start und Target coordinates in degree, returns distance in km
const double f = 1 / 298.257223563; //Abplattung
const double a = 6378.137; //Erdradius in km
double F = degree_to_radian(latt_A + latt_B) / 2;
double G = degree_to_radian(latt_A - latt_B) / 2;
double l = degree_to_radian(long_A - long_B) / 2;
//calculating rough distance
double S = pow(sin(G), 2) * pow(cos(l), 2) + pow(cos(F), 2) * pow(sin(l), 2);
double C = pow(cos(G), 2) * pow(cos(l), 2) + pow(sin(F), 2) * pow(sin(l), 2);
double w = atan(sqrt(S / C));
double D = 2 * w * a; //grober Abstand
//correcting rough distance
double R = sqrt(S * C) / w;
double H_1 = (3 * R - 1) / (2 * C);
double H_2 = (3 * R + 1) / (2 * S);
double s = D * (1 + f * H_1 * pow(sin(F), 2) * pow(cos(G), 2) - f * H_2 * pow(cos(F), 2) * pow(sin(G), 2));
//s = s/1.852; //in nautical miles
// The extra parameter is no longer used.
// When it was NULL, the contents should not be written.
// if( distance != NULL)
// *distance = s;
return s;
}
double distance_WGS84_2(const double & latt_A, const double & long_A, const double & latt_B, const double & long_B) {
//Start und Target coordinates in degree, returns distance in km
const double f = 1 / 298.257223563; //Abplattung
const double a = 6378.137;//Erdradius in km
double F = (latt_A + latt_B) * 0.00872664625997164788461845384244; // PI /180 / 2
double G = (latt_A - latt_B) * 0.00872664625997164788461845384244;
double l = (long_A - long_B) * 0.00872664625997164788461845384244;
double sin_G = sin(G);
double sin_G2 = sin_G * sin_G;
double cos_G2 = 1 - sin_G2; // cos_G * cos_G;
double cos_G = sqrt(cos_G2); // cos(G);
double sin_F = sin(F);
double sin_F2 = sin_F * sin_F;
double cos_F2 = 1 - sin_F2; // cos_F * cos_F;
double cos_F = sqrt(cos_F2); // cos(F);
double sin_l = sin(l);
double sin_l2 = sin_l * sin_l;
double cos_l2 = 1 - sin_l2;
double cos_l = sqrt(cos_l2); // cos(l);
//calculating rough distance
double S = sin_G2 * cos_l2 + cos_F2 * sin_l2;
double C = cos_G2 * cos_l2 + sin_F2 * sin_l2;
double w = atan(sqrt(S / C));
double D = 2 * w * a; //grober Abstand
//correcting rough distance
double R = sqrt(S * C) / w;
double H_1 = (3 * R - 1) / (2 * C);
double H_2 = (3 * R + 1) / (2 * S);
double s = D * (1 + f * H_1 * sin_F2 * cos_G2 - f * H_2 * cos_F2 * sin_G2);
//s = s/1.852; //in nautical miles
// *distance = s;
return s;
}
inline double degree_to_radian(double degree)
{
double radian = degree * PI / 180;
return radian;
}
output:
Duration of floating point calculations
Result : 15551.30 That took : 1428 us
Result : 15551.31 That took : 972 us
Result : 6044.35 That took : 1372 us
Result : 6044.36 That took : 932 us
Result : 2332.87 That took : 1368 us
Result : 2332.87 That took : 936 us
Result : 2786.04 That took : 1380 us
Result : 2786.03 That took : 944 us
Result : 11123.37 That took : 1408 us
Result : 11123.37 That took : 964 us
Result : 10408.16 That took : 1408 us
Result : 10408.16 That took : 960 us
Result : 2470.32 That took : 1372 us
Result : 2470.32 That took : 928 us
Result : 15646.64 That took : 1424 us
Result : 15646.64 That took : 984 us
Result : 14077.65 That took : 1412 us
Result : 14077.65 That took : 980 us
Result : 2026.55 That took : 1388 us
Result : 2026.55 That took : 952 us
Result : 4544.83 That took : 1372 us
Result : 4544.83 That took : 936 us
Result : 18062.88 That took : 1416 us
Result : 18062.88 That took : 992 us
Result : 3123.42 That took : 1388 us
Result : 3123.42 That took : 968 us
Result : 6644.40 That took : 1368 us
Result : 6644.40 That took : 928 us