You can divide things up among multiple treads, e.g. use four threads and calculate the distances for the first 1/4 of the points using thread1, for the second 1/4 of the points using thread2, etc. Just be sure that SparseArray#put
is thread-safe (I don't know what library you're using for this) - if you need to put a lock around the put
method then the program may actually run slower if you split it among multiple threads.
Using single-precision instead of double-precision floating point computation will also speed things up a bit. If you only care about a fixed precision (e.g. two decimal points of precision) then you can use fixed point arithmetic instead, which essentially uses integer arithmetic.
Depending on what your program is doing, you may be able to delay computing the distances to the further points - for example, if you're displaying points to your user 25 at a time, then you can determine the closest 100-or-so points, and then wait to calculate the next 100-or-so points until after the user has scrolled to the 75-100 points; the user may only care about the first 25 points anyway, in which case you never have to compute the distances for the further points. To do this you'll need to index your points using a range tree or k-d tree so that you can quickly determine the closest points to the query point without having to iterate through the entire list; if your points are in a database then you can perform a range query instead. Note that in either case the tree / query is finding the closest points according to their Manhattan distances (delta-lat + delta-lon, instead of delta-lat^2 + delta-lon*2), so you'll still need to calculate their cartesian distances.