Dear Haris,

My timings were on a 3 GHz Pentium 4 system with 1 GB of memory running Win
XP SP2 and R 2.4.1.

I'm no expert on these matters, and I wouldn't have been surprised by
qualitatively different results on different systems, but this difference is
larger than I would have expected. One thing that seems particularly
striking in your results is the large difference between elapsed time and
user CPU time, making me wonder what else was going on when you ran these
examples.

Regards,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
-------------------------------- 

> -----Original Message-----
> From: Charilaos Skiadas [mailto:[EMAIL PROTECTED] 
> Sent: Monday, January 22, 2007 10:00 AM
> To: John Fox
> Cc: r-help@stat.math.ethz.ch; 'miraceti'
> Subject: Re: [R] efficient code. how to reduce running time?
> 
> On Jan 21, 2007, at 8:11 PM, John Fox wrote:
> 
> > Dear Haris,
> >
> > Using lapply() et al. may produce cleaner code, but it won't 
> > necessarily speed up a computation. For example:
> >
> >> X <- data.frame(matrix(rnorm(1000*1000), 1000, 1000)) y <- 
> >> rnorm(1000)
> >>
> >> mods <- as.list(1:1000)
> >> system.time(for (i in 1:1000) mods[[i]] <- lm(y ~ X[,i]))
> > [1] 40.53  0.05 40.61    NA    NA
> >>
> >> system.time(mods <- lapply(as.list(X), function(x) lm(y ~ x)))
> > [1] 53.29  0.37 53.94    NA    NA
> >
> Interesting, in my system the results are quite different:
> 
>  > system.time(for (i in 1:1000) mods[[i]] <- lm(y ~ X[,i]))
> [1] 192.035  12.601 797.094   0.000   0.000
>  > system.time(mods <- lapply(as.list(X), function(x) lm(y ~ x)))
> [1]  59.913   9.918 289.030   0.000   0.000
> 
> Regular MacOSX install with ~760MB memory.
> 
> > In cases such as this, I don't even find the code using *apply() 
> > easier to read.
> >
> > Regards,
> >  John
> 
> Haris
> 
>

______________________________________________
R-help@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.

Reply via email to