I would like to optimize the following code. Currently it runs around 0.085 seconds on a 2Ghz dual core machine with 2MB L2 cache, for M being a 2404 by 100 numeric matrix:
Rescale <- function( M = utility.mat){
exp.M <- exp(M)
result <- apply(exp.M, 1, function(x) x/sum(x))
result <- t(result)
return (result)
}
I have tried replacing apply() with for loop, which gives about the same performance. Any other ideas?
This appears to be a bout 6 times faster on my machine:
I thought that maybe you could speed it up further by calling
.Internal(rowSums())but it didn’t work out. Although that could simply be because I’m not using it correctly.