Re: [R] help:too slow

About this list Date view Thread view Subject view Author view

From: Peter Dalgaard BSA (p.dalgaard@biostat.ku.dk)
Date: Wed 21 Mar 2001 - 09:15:02 EST


Message-ID: <x2y9u0rq15.fsf@blueberry.kubism.ku.dk>

detlef.steuer@unibw-hamburg.de writes:

> Hi!
>
> Using your original function on some dummy data I get:
> (700MHz PIII Linux 2.2.18)
>
> w <- as.matrix(floor(runif(100000,100,999)))
> unix.time(m <- time(col2mat(w)))
> [1] 22.67 0.69 50.48 0.00 0.00
>
> 50 seconds to generate the whole matrix.
>
> If I comment out the print(s) it reduces to:
> unix.time(m <- time(col2mat(w)))
> [1] 21.03 0.64 21.86 0.00 0.00
>
> But if I change the function definition to
>
> "col2mat" <-
> function(x, sampsz=220, qsamp=2000) {
> m<-matrix(nr=qsamp, nc=sampsz)
> for(s in 1:qsamp){
> ### print(s)
> ### for(i in 1:sampsz){
> m[s,(1:sampsz)] <- x[s+(1:sampsz),1]
> ### }
> }
> m
> }
>
> which I consider functional equivalent I can go down to:
> > unix.time(m <- time(col2mat(w)))
> [1] 0.52 0.06 0.59 0.00 0.00
>
> Probably you can get rid of the outer loop in a similar fashion.
> Hope that helps,

The expedient solution would seem to be

col2mat <- function(x, sampsz=220, qsamp=2000)
   outer(1:qsamp, 1:sampsz, function(i,j) x[i+j,1])

although it doesn't actually seem much faster than Detlef's
version.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view

This archive was generated by hypermail 2b30 : Fri 22 Jun 2001 - 18:58:36 EST