[Rcpp-devel] R.e. Speed gain assistance (Wray, Christopher)

Hadley Wickham hadley at rice.edu
Thu Aug 18 16:57:10 CEST 2011


> Take a look at this (unweighted) sample() function.  It's giving R a
> run for it's money, and is pretty fast even for very large n, and it
> looks statistically correct (not sure if I'm glossing over ugly,
> machine-specific details of double->int conversion here). Does this
> shed any light on your question?
> mysample<-cxxfunction( signature(x='numeric', n="numeric"), src1, plugin='Rcpp')
>
> system.time(result <- mysample(1:50, 1e7))
> system.time(resultR <- sample(1:50, 1e7, replace=T))

Don't forget about sample.int:

> system.time(result <- mysample(1:50, 1e7))
   user  system elapsed
  0.493   0.064   0.557
> system.time(resultR <- sample(1:50, 1e7, replace=T))
   user  system elapsed
  0.872   0.089   0.962
> system.time(resultR <- sample.int(1:50, 1e7, replace=T))
   user  system elapsed
  0.209   0.001   0.212

Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/


More information about the Rcpp-devel mailing list