[R-sig-eco] Improving efficiency in a simulation model

tyler tyler.smith at mail.mcgill.ca
Fri Oct 10 03:41:10 CEST 2008

"Dulce M. Bustamante" <dbusta1 at ufl.edu> writes:

> I have written R functions before for simple simulations, but this
> model will have more complexities and each individual will need to be
> evaluated in many aspects at each time step. My major concern is that
> I don't know how to make my program more efficient in terms of using
> computer memory and computation time.

gc() will give you a snapshot of the current memory use of R. Rprof and
related functions in the proftools library provide ways to identify
the parts of your simulation that are particularly time-consuming. These
are the bits you may want to convert to C. The help files for these
functions has pointers to other useful tools. Also, see the 'Writing R
Extensions manual'.

> I have read that R could interact with C and that that can be an
> "extremely effective mode for numerical computation" (Bates and
> DebRoy, DSC 2003 Working Papers). I have looked for papers on HOW TO
> make R and C interact but I can not find many references and would
> appreciate suggestions.

The 'Writing R Extensions' manual that comes with the standard
distribution includes lots of info on passing data back and forth
between R and C. C code runs a lot faster than R, especially when you've
got a lot of loops. On the other hand, converting R loops to R
vector/matrix operations can make a big difference too. 

> Would my model benefit from using object oriented programming in terms
> of making the procedures go faster in terms of updating matrices after
> time steps?

I don't know if OO will make your programs run any faster, but it may make
them easier to understand and maintain.




More information about the R-sig-ecology mailing list