for - if 반복문 성능을 올리는 법

게시일 : 2017년 11월 26일    
# R

2016년 1월 작성된 R-bloggers의 글을 정리한다. –

약간의 코드 변화만으로도 성능을 매우 높일 수 있다.

모든 그래프는 Mac OS X with 2.6GHz & 8GB RAM에서 테스트한 기록이다.

# Raw Code
for( i in 1:nrow(df) ){
    if ( condition )
        df[i, 5] <- "True"
    else
        df[i, 5] <- "False" }

df : 4개의 컬럼을 가진 dataframe

condition : df값을 활용한 조건문

Output 벡터 선언(Vectorization)

# Output을 저장할 벡터 선언
output <- vector('character', length=nrow(df))
for( i in 1:nrow(df) ){
    if ( condition )
        output[i] <- "True"
    else
        output[i] <- "False" }
# Output을 새로운 컬럼으로 할당
df$new_col <- output

vectiorization

for문 대신 apply 함수를 사용할 수도 있지만 apply 함수가 훨씬 느리다.

vectiorization

for 문 밖에 조건 변수(condition) 선언

cond : logical 벡터

e.g.) logi [1:5] FALSE FALSE TRUE FALSE FALSE

# condition 벡터 생성
output <- vector('character', length=nrow(df))
cond <- condition
for( i in 1:nrow(df) ){
    if ( cond[i] ) 
        output[i] <- "True"
    else
        output[i] <- "False" }
df$new_col <- output        

condition

True 조건에 맞는 row만 실행

output <- vector('character', length=nrow(df))
cond <- condition
# for 문을 실행할 row 설정
for( i in ( 1:nrow(df) )[ cond ] ){
    if ( cond[i] ) 
        output[i] <- "True"
    else
        output[i] <- "False" }
df$new_col <- output   

true_condition

ifelse문 사용 ( 간단한 for문을 작성할 시 )

지금까지 했던 방법을 활용하지 않고 ifelse로만 작성한다.

# output을 미리 할당하지 않고 바로 계산
output <- ifelse( condition, "True", "False" )
df$new_col <- output

ifelse

Rcpp ( 추후 공부가 필요 )

Rcpp는 C++을 이용하는 방법으로 매우 빠른 속도를 보여준다.

rcpp

기타 방법으로는,

  • Parallel Processing

  • 반복문 끝에 garbage collector를 실행하는 gc()를 사용

  • 코드 중간 필요없는 object를 삭제하는 rm() 활용

  • data.frame 대신 data.table 사용

Link :