tag:blogger.com,1999:blog-7060659261993016566.post1334147525567341637..comments2017-01-04T04:59:21.665-06:00Comments on A second megabyte of memory: An RcppEigen exampleDouglas Bateshttp://www.blogger.com/profile/16223568223556405423noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-7060659261993016566.post-64025944847878351322012-03-13T07:37:06.532-05:002012-03-13T07:37:06.532-05:00Sorry, for the deleted post. I had an error in the...Sorry, for the deleted post. I had an error in the original post. Here is a correct (and better) version: <br /><br />After playing around with the example I came up with a much faster pure R version (although still slower than with Rcpp). The speedup is mainly from the vectorized operations instead of individual function calls for each row. In other words: better loop over the "short" dimension and treat the other one in vector ops. On top of that, row-wise loop over a matrix that is stored in column-major fashion is suboptimal, too, but in this case probably negligible.<br /><br />Rsamp2<-function(X) {<br /> stopifnot(is.numeric(X <- as.matrix(X)),<br /> (nc <- ncol(X)) > 1L,<br /> all(X >= 0))<br /> R<- runif(nrow(X))<br /> I<-rep(1L, nrow(X))<br /> for(i in 1:(ncol(X)-1)) {<br /> R<-R-X[,i]<br /> I<-I+(R>=0)<br /> }<br /> I<br />}<br /><br />set.seed(1234321)<br />X <- matrix(runif(100000 * 5), ncol=5)<br />benchmark(Rsamp(X), Rsamp2(X), RcppSamp(X), replications=10, columns=c("test", "elapsed", "relative", "user.self"))<br /> test elapsed relative user.self<br />3 RcppSamp(X) 0.134 1.000000 0.132<br />1 Rsamp(X) 8.315 62.052239 8.288<br />2 Rsamp2(X) 0.262 1.955224 0.260Jonas Rauchhttps://www.blogger.com/profile/11749224260132470331noreply@blogger.comtag:blogger.com,1999:blog-7060659261993016566.post-27136292953558148892012-03-13T07:17:57.678-05:002012-03-13T07:17:57.678-05:00This comment has been removed by the author.Jonas Rauchhttps://www.blogger.com/profile/11749224260132470331noreply@blogger.comtag:blogger.com,1999:blog-7060659261993016566.post-76367867958775351832012-03-13T05:57:53.284-05:002012-03-13T05:57:53.284-05:00Thanks! I'll check it out later today.Thanks! I'll check it out later today.Jonas Rauchhttps://www.blogger.com/profile/11749224260132470331noreply@blogger.comtag:blogger.com,1999:blog-7060659261993016566.post-43429168553287271002012-03-12T12:14:38.966-05:002012-03-12T12:14:38.966-05:00I posted a package at http://www.stat.wisc.edu/~ba...I posted a package at http://www.stat.wisc.edu/~bates/Chung_0.1-1.tar.gz<br /><br />You need to have RcppEigen-0.2.0, which has just been added to CRAN, installed.Doug Bateshttps://www.blogger.com/profile/16223568223556405423noreply@blogger.comtag:blogger.com,1999:blog-7060659261993016566.post-80179714847619526682012-03-12T11:19:33.911-05:002012-03-12T11:19:33.911-05:00Indeed, I should have explained that. I was copyi...Indeed, I should have explained that. I was copying Dongjin's code and he believed that the sample function in R did not allow probabilities of zero. However, I just checked and apparently it does allow them<br /><br />> sample(5, 1, prob=c(0.1, 0, 0.3, 0.4, 0.2))<br />[1] 4<br /><br />so I can simplify the R version of the code.Doug Bateshttps://www.blogger.com/profile/16223568223556405423noreply@blogger.comtag:blogger.com,1999:blog-7060659261993016566.post-70673780647659460322012-03-12T01:48:48.602-05:002012-03-12T01:48:48.602-05:00Douglas, could you post your full working example?...Douglas, could you post your full working example? I would love to play with it a little.Jonas Rauchhttps://www.blogger.com/profile/11749224260132470331noreply@blogger.comtag:blogger.com,1999:blog-7060659261993016566.post-89960446348323664132012-03-11T21:21:00.706-05:002012-03-11T21:21:00.706-05:00I don't get why the probabilities are `x+1e-10...I don't get why the probabilities are `x+1e-10` and not just `x`, but I assume it's some floating-point roundoff thing. Could you say more about that? And it doesn't seem to apply the same technique in the Rcpp code, is that right?Kenahoohttps://www.blogger.com/profile/03715130777653364565noreply@blogger.com