While 루프

Rcpp로 R 코드 최적화하기

Romain François

Consulting Datactive, ThinkR

While 루프는 더 단순합니다

while( condition ){
    body
}
  • 계속 조건
  • 루프 본문
Rcpp로 R 코드 최적화하기
// [[Rcpp::export]]
int power( int n ){    
    if( n < 0 ){
        stop( "n must be positive" )  ;
    }
    int value = 1 ;
    while( value < n ){
        value = value * 2 ;
    }
    return value ;
}

sourceCpp로 함수를 컴파일한 후 호출할 수 있습니다:

power( 1000 )
power( 17 )
1024
32
Rcpp로 R 코드 최적화하기

For 루프는 While 루프입니다

for( init ; condition; increment ){
    body
}

는 다음과 동일합니다

init
while( condition ){
    body
    increment
}
Rcpp로 R 코드 최적화하기

do / while 루프

do {
    body
} while( condition ) ;
Rcpp로 R 코드 최적화하기

do / while 루프 예제

// [[Rcpp::export]]
int power( int n ){

    if( n < 0 ){
        stop( "n must be positive" )  ;
    }

    int value = 1 ;
    do {
        value = value * 2 ;
    } while( value < n );
    return value ;
}
Rcpp로 R 코드 최적화하기

연습해 봅시다!

Rcpp로 R 코드 최적화하기

Preparing Video For Download...