Skip to content
Back to formatted view

Raw Message

Message-ID: <CABdHhvGCqjhrte6YzTFhu8AvVjK_AO2xQO2giHO=yTuSTiaoPQ@mail.gmail.com>
Date: 2011-08-18T14:57:10Z
From: Hadley Wickham
Subject: [Rcpp-devel] R.e. Speed gain assistance (Wray, Christopher)
In-Reply-To: <CADNH-Pu9x9Z0GMH5NObhoQ4aMd0tfFzy66Sawcs_mvC3uUMY7A@mail.gmail.com>

> 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/