nimble-dev / nimble

The base NIMBLE package for R
http://R-nimble.org
BSD 3-Clause "New" or "Revised" License
156 stars 23 forks source link

use `inherits` in `nf_preProcessMemberDataObj` #1413

Closed paciorek closed 5 months ago

paciorek commented 7 months ago

Using inherits is faster than is in general, I believe.

Using it in nf_preProcessMemberDataObj can give a surprisingly large (20%) speedup in a case where we are building many simple samplers. In code below buildMCMC takes 28 sec when using inherits and 36 sec when using is.

n <- 10000
code <- nimbleCode({
    for(i in 1:n) {
        y[i]~dpois(mu[i])
        mu[i]~dnorm(mu0,sd=sigma)
    }
    mu0 ~ dnorm(0,sd=10)
    sigma ~ dunif(0, 10)
})

m <- nimbleModel(code, data = list(y = rpois(n,1)), constants = list(n=n))
system.time(conf <- configureMCMC(m, useConjugacy=FALSE))
system.time(mcmc <- buildMCMC(conf))