2014-04-29 6 views
1

누군가 여기서 나에 대해 설명 할 수 있습니까? ,
이 나는 ​​테이블과 일치 할 필요는리스트의 목록을 가지고 있고, 나는 테이블을 해시하여 일치하는 용도가 일치 할 생각에 대한 fmatch (패키지 fastmatch http://cran.r-project.org/web/packages/fastmatch/index.html) (와 lapply을 사용하고 있습니다 과 일치합니다.
그러나 테이블 값을 함수에서 평가해야하는 경우에는 다소 느립니다 (적어도 의심 스럽지만).
계산 속도를 5.5에서 0.01s로 높이는 해결 방법을 찾았지만 좀 더 우아한 솔루션을 원합니다.
여기 는 재현 예입니다
이상한 bahaviour와 lapply, 게으른 평가?

set.seed(10) 

matchFeatures <- replicate(n = 1000, paste0("a", sample(x = 1:10000, size = sample(x = 1:10, size = 1)))) 
matchTable <- 1:10000 

system.time(m1 <- lapply(matchFeatures, function(features) fmatch(features, paste0("a", 1:10000)))) 
system.time(m2 <- lapply(matchFeatures, function(features) force(fmatch(features, paste0("a", 1:10000))))) 
system.time({tempTable <- paste0("a", 1:10000); m3 <- lapply(matchFeatures, function(features) fmatch(features, tempTable))}) 
identical(m1, m3) 

감사 저스틴, 그냥 따라, 나는 이런 식으로 뭔가를 찾고 있었다 : 첫 번째 두 가지 기능에

system.time(m4 <- lapply(matchFeatures, fmatch, table = paste0("a", 1:10000))) 

답변

2

, 실행하고있는 paste 명령은 각 반복 (즉, 10000 회)마다 한 번씩. 세 번째는 한 번만 발생합니다. matchTable <- paste('a', 1:10000)을 사용하고 세 가지 버전 모두에 matchTable을 전달하면 예상대로 상당한 속도가 향상됩니다.

matchFeatures <- replicate(n = 1000, 
          paste0("a", 
          sample(x = 1:10000, 
            size = sample(x = 1:10, size = 1)))) 
matchTable <- paste('a', 1:10000) 

system.time(m1 <- lapply(matchFeatures, 
         function(features) fmatch(features, matchTable))) 
system.time(m2 <- lapply(matchFeatures, 
         function(features) force(fmatch(features, matchTable)))) 
system.time(m3 <- lapply(matchFeatures, 
         function(features) fmatch(features, matchTable))) 
identical(m1, m3) 
+0

감사합니다. – arunasm