stu.csv包含850,000行和3列。第二列是ID的經度,第三列是ID的緯度。 stu.csv文件中的數據如下:R代碼運行速度太慢,如何加速和重寫此代碼
ID longitude latitude
156 41.88367183 12.48777756
187 41.92854333 12.46903667
297 41.89106861 12.49270456
89 41.79317669 12.43212196
79 41.90027472 12.46274618
... ... ...
該僞代碼如下。它的目的是計算與經度和緯度的地球表面上的兩個ID之間的距離,以及從任何兩個ID輸出的累積和:
dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = 6371000 * c (where 6371000 is the radius of the Earth)
此代碼如下,但它運行太慢。如何加速和重寫代碼?謝謝。
stu<-read.table("stu.csv",header=T,sep=",");
## stu has 850,000 rows and 3 columns.
m<-nrow(stu);
distance<-0;
for (i in 1:(m-1))
{
for (j in (i+1))
{
dlon = stu[j,2] - stu[i,2];
dlat = stu[j,3] - stu[i,3];
a = (sin(dlat/2))^2 + cos(stu[i,3]) * cos(stu[j,3]) * (sin(dlon/2))^2;
c = 2 * atan2(sqrt(a), sqrt(1-a));
distance <-distance+6371000 * c;
}
}
distance
一個O上約0.4秒(N^2)算法總是要當N = 850000 .... –
@ chinsoon12感謝您花很長的時間你的回覆。我描述了問題 – user2405694
@ chinsoon12是 – user2405694