[R] memory management uestion
Charles C. Berry
cberry at tajo.ucsd.edu
Mon Feb 19 21:58:20 CET 2007
On Mon, 19 Feb 2007, Federico Calboli wrote:
> Hi All,
>
> I would like to ask the following.
>
> I have an array of data in an objetct, let's say X.
>
> I need to use a for loop on the elements of one or more columns of X and I am
> having a debate with a colleague about the best memory management.
Yez guys should take this fight out into the parking lot. ;-)
Armed with gc(), system.time(), and whatever memory monitoring tools your
OS'es provide you can pound each other with memory usage and timing stats
till one of you screams 'uncle' or you both have had enough and decide to
shake hands and come back inside.
>
> I believe that if I do:
>
> col1 = X[,1]
> col2 = X[,2]
> ...
> colx = X[,x]
>
>
> and then
>
> for(i in whatever){
> do something using col1[i], col2[i] ... colx[i]
> }
>
> my memory management is better that doing:
>
> for(i in whatever){
> do something using X[i,1], X[i,2] ... X[,x]
> }
>
Whoa! You are accessing one ROW at a time.
Either way this will tangle up your cache if you have many rows and
columns in your orignal data.
You might do better to do
Y <- t( X ) ### use '<-' !
for (i in whatever ){
do something using Y[ , i ]
}
> BTW, here I *have to* use a for() loop an no nifty tapply, lapply and family.
>
> Any comment is welcome.
>
> Best,
>
> Fede
>
> --
> Federico C. F. Calboli
> Department of Epidemiology and Public Health
> Imperial College, St Mary's Campus
> Norfolk Place, London W2 1PG
>
> Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193
>
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list