어떻게하면 다음 코드를 더 빠르게 만들 수 있습니다. 지금까지, P = 1 (즉, 하나의 루프)에 대한 전체 프로세스는 약 15 분이 걸린다. 나는 문제가 For
루프와 함께 있어야한다는 것을 알고 있으며 그것에 관한 몇 가지 관련 질문을 이미 읽었지만 어떻게 작동하는지 이해할 수 없었다. 스크립트 다음For 루프 최적화 - R
: P와 R은 약 1,000하고 TOLTarget 및 TOLSource은 500
어떤 도움 감사 드리겠습니다 최대가 될 수 있습니다.
for(i in 1:P)
{
Source <- MITLinks[i,1]
Target <- MITLinks[i,2]
TOLTarget <- sum(!is.na(MITMatrix[Target,]))-1 # TOLTarget would be the number of concepts for the target course
TOLSource <- sum(!is.na(MITMatrix[Source,]))-1
for(q in 2:TOLSource) # since the first coulmn is the courseID
{
DD <- vector(length = R)
ConceptIDSource <- MITMatrix[Source,q]
counterq <- 1 # counterq is a pointer to cell of vector DD that keep the corses from another university.
for(c in 1:R)
{
if(CALBinary[c,match(ConceptIDSource,BB)]==1) # if(CALBinary[c,"ConceptIDSource"]==1)
{
DD[counterq] <- c # it is the courseID
counterq <- counterq+1
}
}
DD <- DD[ DD != 0 ] # DD is a vector that keep all courses from another university hat share the same concepts as source course in the first university (MIT)
for(j in 2:TOLTarget) # Since the first coulmn is the courseID
{
ZZ <- vector(length = R)
ConceptIDTarget <- MITMatrix[Target,j]
counter <- 1
for(v in 1:R)
{
if(CALBinary[v,match(ConceptIDTarget,BB)]==1) #if(CALBinary[v,"ConceptIDTarget"]==1)
{
ZZ[counter] <- v # v is courseID
counter <- counter+1
}
}
ZZ <- ZZ[ ZZ != 0 ] # delete the zero elements from the vector
Jadval<- expand.grid(Source,Target,ConceptIDSource,ConceptIDTarget,DD,ZZ)
Total<-rbind(Total,Jadval) # to make all possible pair of the courses for the sorce and the target course
Total
}
}
}
코드 최적화 질문은 CodeReview 대신 StackOverflow http://codereview.stackexchange.com/ –
에 문의해야합니다. 또한 코드에 som 배경을 포함시켜야합니다. 귀하의 코드는 무엇이며 왜 그럴까요? – N3buchadnezzar