Skip to content
Prev 317349 / 398502 Next

improving/speeding up a very large, slow simulation

Hi Ben,

I appreciate that the example is reproducible, and I understand why
you shared the entire project.  At the same time, that is an awful lot
of code for volunteers to go through and try to optimize.

I would try to think of the structure of your task, see what the
individual pieces are---figure out what can be reused and optimized.
Look at loops and try to think whether some operation performed on
every element of the vector at once could do the same thing.
Sometimes the next iteration of a loop depends on the previous, so it
is difficult to vectorize, but often that is not the case.

Make use of the compiler package, especially cmpfun().  It can give
fairly substantial speedups, particularly with for loops in R.

Finally if you want 1000 reps, do you actually need to repeat all the
steps 1000 times, or could you just simulate 1000x the data?  If the
latter, do the steps once but make more data.  If the former, you
ought to be able to parallelize it fairly easily, see the parallel
package.

Good luck,

Josh


On Mon, Feb 11, 2013 at 9:22 PM, Benjamin Caldwell
<btcaldwell at berkeley.edu> wrote:
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/