2016-06-26 5 views
0

어떻게하면 다음 코드를 더 빠르게 만들 수 있습니다. 지금까지, 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 
    } 
    } 
} 
+0

코드 최적화 질문은 CodeReview 대신 StackOverflow http://codereview.stackexchange.com/ –

+0

에 문의해야합니다. 또한 코드에 som 배경을 포함시켜야합니다. 귀하의 코드는 무엇이며 왜 그럴까요? – N3buchadnezzar

답변

1

이 코드를 개선하고 더 빠르게 만들 수있는 영역이 많이 있습니다. 기본적으로 C 스타일 코드를 작성하고 내장 된 벡터화 된 R 함수를 사용하지 않는 것 같습니다. 한 가지 예가 있습니다. 코드의이 부분 :

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 ] 

은 다음과 같이 수행 할 수 있습니다 : 당신의 코드에서

ConceptIDSource <- MITMatrix[Source,q] 
CalBinaryBB <- CALBinary[,match(ConceptIDSource,BB)] 
DD<-which(CalBinaryBB[1:R]==1) 

, 당신은 불필요 루프를 통해 때마다 match을 요구하고있다. 그리고 당신이하고있는 모두가 CALBinary[c,match(ConceptIDSource,BB)]==1 인 인덱스를 찾기 위해 노력하고 있기 때문에, R 함수 which이 이것을 훨씬 빨리 수행 할 것입니다.

루프의 두 번째 부분에서 똑같은 작업을 수행 할 수있는 것처럼 보입니다. 그리고 최적화를위한 다른 기회가있을 수 있습니다.