[R] help:too slow

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Mar 21 00:15:02 CET 2001


detlef.steuer at 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 at 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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list