I am computing distance matrix between large number of locations (5000) on sphere (using Haversine distance function).
Here is my code:
require(geosphere)
x=rnorm(5000)
y=rnorm(5000)
xy1=cbind(x,y)
The time taken for computing the distance matrix is
system.time( outer(1:nrow(xy1), 1:nrow(xy1), function(i,j) distHaversine(xy1[i,1:2],xy1[j,1:2])))
The time taken to execute this program is high. Any suggestion how to lower time consumption to do this job! Thanks.
for
loop to cycle through a vector to repeatedly call a function (distHaversine()
) which is already vectorised!! They wrote more code whilst also slowing the speed of execution by about 300X!!! Do not heed this article! You don't call a function 10,000 times when once will do! – Banka