[R-br] sugestao de melhorias em codigo?

Henrique Dallazuanna wwwhsd em gmail.com
Terça Março 29 12:04:37 BRT 2011


Gustavo,

A saida de alguma funçao não é igual a outra:

Reduce(identical, list(R1, R2, R3, R4, R5))

2011/3/29 Gustavo Henrique de Carvalho <gustavo.bio em gmail.com>:
> Mudei a chamada da função mean no código acima e adicionei loops
> aninhados para comparar. Prestem atenção ao tamanho das matrizes de
> entrada, pode ser que demore pra rodar:
>
> set.seed(1)
> X <- matrix(rnorm(30000), nc=5)
> Y <- matrix(rnorm(15000), nc=5)
>
> f = function(i, x, y){
>  n = ncol(x)
>  sapply(1:n, function(j, v, ref) .Internal(mean(ref[,j] > v[j])), x[i,], y)
> }
>
> t1 <- system.time(
>  R1 <- t(sapply(1:nrow(X), f, X, Y))
> )
>
> t2 <- system.time(
>  R2 <- mapply(function(x, y)colMeans(sapply(x, function(.x).x < y)),
>    as.data.frame(X), as.data.frame(Y))
> )
>
> t3 <- system.time(
>  R3 <- mapply(function(x, y) sapply(x, function(.x) mean(.x <
>          y)),
>    as.data.frame(X), as.data.frame(Y))
> )
>
> t4 <- system.time(
>  R3 <- mapply(function(x, y) sapply(x, function(.x) .Internal(mean(.x <
>          y))),
>    as.data.frame(X), as.data.frame(Y))
> )
>
> blah <- function(X, Y) {
>  R <- X
>  for (j in 1:NCOL(X)) {
>    bleh <- Y[, j]
>    for (i in 1:NROW(X)) {
>      R[i,j] <- .Internal(mean(X[i,j] < bleh))
>    }
>  }
>  R
> }
>
> t5 <- system.time(R5 <- blah(X, Y))
>
> t1
> t2
> t3
> t4
> t5
>
> Abraços,
>
> Gustavo
>
> 2011/3/29 Benilton Carvalho <beniltoncarvalho em gmail.com>
>>
>> aha! sou mestre em esquecer do mapply... obrigado...
>>
>> FYI: eu tenho um ganho significativo com:
>>
>> mapply(function(x, y) sapply(x, function(.x) mean(.x < y)),
>>                   as.data.frame(X), as.data.frame(Y))
>>
>> obrigado, b
>>
>> 2011/3/29 Henrique Dallazuanna <wwwhsd em gmail.com>:
>> > Benilton,
>> >
>> > Tente com essa, aparenta ser 30% mais rapida:
>> >
>> > mapply(function(x, y)colMeans(sapply(x, function(.x).x < y)),
>> > as.data.frame(X), as.data.frame(Y))
>> >
>> > #benchmark
>> > t1 <- system.time(replicate(100, t(sapply(1:nrow(X), f, X, Y))))
>> > t2 <- system.time(replicate(100, mapply(function(x,
>> > y)colMeans(sapply(x, function(.x).x < y)), as.data.frame(X),
>> > as.data.frame(Y))))
>> >
>> > t2 / t1
>> >
>> > 2011/3/29 Benilton Carvalho <beniltoncarvalho em gmail.com>:
>> >> Prezados,
>> >>
>> >> existe algo obvio (em termos de R, visto que gostaria de evitar o uso
>> >> de codigo compilado) que eu esteja esquecendo em termos de otimizacao
>> >> para a seguinte operacao?
>> >>
>> >> Ela se resume a:
>> >>
>> >> R[i,j] = mean( X[i,j] < Y[,j])
>> >>
>> >> O CMR segue:
>> >>
>> >> set.seed(1)
>> >> X <- matrix(rnorm(100), nc=5)
>> >> Y <- matrix(rnorm(50), nc=5)
>> >>
>> >> f = function(i, x, y){
>> >>  n = ncol(x)
>> >>  sapply(1:n, function(j, v, ref) mean(ref[,j] > v[j]), x[i,], y)
>> >> }
>> >> R = t(sapply(1:nrow(X), f, X, Y)
>> >>
>> >>
>> >> O problema que surge para mim refere-se ao tempo de execucao quando
>> >> tenho q lidar com X com dimensoes 5e6 x 1e3.... pois, naturalmente, a
>> >> solucao acima levara' um bom tempo para finalizar...
>> >>
>> >> obrigado,
>> >> benilton
>> >> _______________________________________________
>> >> R-br mailing list
>> >> R-br em listas.c3sl.ufpr.br
>> >> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
>> >>
>> >
>> >
>> >
>> > --
>> > Henrique Dallazuanna
>> > Curitiba-Paraná-Brasil
>> > 25° 25' 40" S 49° 16' 22" O
>> >
>> _______________________________________________
>> R-br mailing list
>> R-br em listas.c3sl.ufpr.br
>> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O


Mais detalhes sobre a lista de discussão R-br