You've already got an answer as to how to use more than one core, but the real problem is with the way you have written your loops. Never extend your result vector/object at each iteration of a loop. If you do this, you force R to copy your result vector/object and extend it which all takes time. Instead, preallocate enough storage space before you start the loop and fill in as you go along. Here is an example:
set.seed(1)
p1 <- matrix(rnorm(10000), ncol=100)
system.time({
p1max <- p1mean <- p1sum <- numeric(length = 100)
for(i in seq_along(p1max)){
p1max[i] <- max(p1[i,])
p1mean[i] <- mean(p1[i,])
p1sum[i ]<- sum(p1[i,])
}
})
user system elapsed
0.005 0.000 0.005
Or you can do these things via apply():
system.time({
p1max2 <- apply(p1, 1, max)
p1mean2 <- apply(p1, 1, mean)
p1sum2 <- apply(p1, 1, sum)
})
user system elapsed
0.007 0.000 0.006
But note that this is no faster than doing the loop properly and sometimes slower.
However, always be on the lookout for vectorised code. You can do row sums and means using rowSums() and rowMeans() which are quicker than either the loop or apply versions:
system.time({
p1max3 <- apply(p1, 1, max)
p1mean3 <- rowMeans(p1)
p1sum3 <- rowSums(p1)
})
user system elapsed
0.001 0.000 0.002
If I were a betting man, I would have money on the third approach I mention beating foreach() or the other multi-core options in a speed test on your matrix because they would have to speed things up considerably to justify the overhead incurred in setting up the separate processes that are farmed out the the different CPU cores.
Update: Following the comment from @shabbychef is it faster to do the sums once and reuse in the computation of the mean?
system.time({
p1max4 <- apply(p1, 1, max)
p1sum4 <- rowSums(p1)
p1mean4 <- p1sum4 / ncol(p1)
})
user system elapsed
0.002 0.000 0.002
Not in this test run, but this is far from exhaustive...