Writing Efficient R Code
Colin Gillespie
Jumping Rivers & Newcastle University
So where can parallel computing help?
for(i in 1:8)
sims[i] <- monte_carlo()
# User defined function
combine_simulations(sims)
x <- 1:8
for(i in 2:8)
x[i] <- x[i-1]
x[8] = x[7] = ... x[2] = x[1] = 1
x[3]
may get evaluated before x[2]
Can the loop be run forward and backwards?
for(i in 1:8)
sim[i] <- monte_carlo_simulation()
for(i in 8:1)
sim[i] <- monte_carlo_simulation()
Can the loop be run forward and backwards?
x <- 1:8
for(i in 2:8)
x[i] <- x[i-1]
for(i in 8:2)
x[i] <- x[i-1]
x[8] = x[7] = ... = 1
x[8] = x[7] = 7
Remember: If you can run your loop in reverse, you can probably use parallel computing.
Writing Efficient R Code