I have a really bad effiency problem. I need to get the furthest distance between a set of points and my first "brute force" algorithm takes almost 80 seconds. I need it to happen within 1 second. Worst case scenario is moving the calculations into background processes and multithreading them but it still needs to be faster so here's my first ever stackoverflow question..
The data I have are 39 000 sets of coordinates, each set contains about 200 x,y coordinates and I'm looking for the furthest distance in each set.
The datapoints are represented by x and y and I'm computing the distance between them using Math.Sqrt(deltaX * deltaX + deltaY * deltaY)
The datapoints can be in any order.
My brute force attempt looks like this
public static double getAbsoluteMax(IEnumerable<DataPoint> dataPoints)
{
double maxDistance = 0;
foreach (DataPoint dp1 in dataPoints)
{
foreach (DataPoint dp2 in dataPoints)
{
double deltaX = dp1.x - dp2.x;
double deltaY = dp1.y - dp2.y;
double distance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > maxDistance)
{
maxDistance = distance;
}
}
}
return maxDistance;
}
I am calling this function with 200 values each time.. 39 000 times.
My first thought was Memoize that is found in Perl, it caches the results of any method call and then looks it up if the same method is called with the same parameters. Maybe creating a lookup table with results from the math could help?
Maybe I could move the calculations to matlab or something similar ?
The application is .net 4.5 and the calculations are in a .net 4.5 dll