[R] For-Loop faster than vectorized code?
Ralph79
ralph.statistics at gmx.net
Fri Jan 25 13:30:19 CET 2008
Dear R-Users,
I am working on an Hierarchical Bayes model and tried to replace the inner
for-loop (which loops over a list with n.observations elements) with truely
vectorized code (where I calculated everything based on ONE dataset over all
respondents).
However, when comparing the performance of the two alternatives, I found out
that the code with the for-loop actually was faster! In order to see where
my vectorized code fails, I used the Rprof-function and got the following
output:
> summaryRprof("testfunc")
$by.self
self.time self.pct total.time total.pct
inherits 0.16 34.8 0.34 73.9
* 0.14 30.4 0.14 30.4
rowSums 0.06 13.0 0.36 78.3
exp 0.04 8.7 0.04 8.7
as.vector 0.02 4.3 0.40 87.0
colSums 0.02 4.3 0.04 8.7
file.info 0.02 4.3 0.02 4.3
testfunc 0.00 0.0 0.46 100.0
ll.md.ub.mat 0.00 0.0 0.44 95.7
matrix 0.00 0.0 0.40 87.0
is.data.frame 0.00 0.0 0.34 73.9
source 0.00 0.0 0.02 4.3
srcfile 0.00 0.0 0.02 4.3
$by.total
total.time total.pct self.time self.pct
testfunc 0.46 100.0 0.00 0.0
ll.md.ub.mat 0.44 95.7 0.00 0.0
as.vector 0.40 87.0 0.02 4.3
matrix 0.40 87.0 0.00 0.0
rowSums 0.36 78.3 0.06 13.0
inherits 0.34 73.9 0.16 34.8
is.data.frame 0.34 73.9 0.00 0.0
* 0.14 30.4 0.14 30.4
exp 0.04 8.7 0.04 8.7
colSums 0.04 8.7 0.02 4.3
file.info 0.02 4.3 0.02 4.3
source 0.02 4.3 0.00 0.0
srcfile 0.02 4.3 0.00 0.0
$sampling.time
[1] 0.46
If I interpret this output right, my main problem is the function
ll.md.ub.mat, which needs about 95% of the total time required. When
profiling this function alone, I clearly see that most of the time is spent
on "inherits" and "*".
The "*" is used in an elementwise multiplication of two matrices. Is there a
way to speed that up?
And what exactly does "inherits" mean, which seems to be a quite
time-consuming part?
Based on that Rprof-output, can anyone give me a hint on where to optimise
my code?
And why is it possible that the calculation is faster when conducted based
on loops? Might there be a memory problem due to the huge matrices (one
[240000 x 25] instead of 800 [300 x 25]-matrices) I have to work with in the
vectorized code?
My system is a Core 2 Duo E6600 with 2x2.4 GHz and 2GB RAM. However, I get a
new PC with 4 GB RAM soon. Maybe that would help?
Thanks for your suggestions in advance!
Ralph
-----
Ralph Wirth
University Erlangen-Nuremberg, Chair of Statistics
GfK Group, Department of Methods and Product Development
--
View this message in context: http://www.nabble.com/For-Loop-faster-than-vectorized-code--tp15086751p15086751.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list