[R] help:too slow

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Mar 20 10:34:03 CET 2001



"Valery A.Khamenya" wrote:
> 
> here is a script I use to make overlapping cut of my input "col.dat"
> file into a matrix:
> 
> #----------------------------------
> 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,i] <- x[s+i,1]
>     }
>   }
>   m
> }
> w<-read.table("col.dat", check.names=FALSE)
> m<-col2mat(w)
> #----------------------------------
> 
> the input file "col.dat" looks like this:
> #-----------------------
> 118
> 118
> 122
> .
> .
> .
> -90
> -84
> -120
> -108
> #------------
> contains about 380000 numbers and has a size of 1.5Mb. Not a small
> file I understand, but not too large! However I do not use the whole
> file at _this_ stage of my task, I use no more than 10% (no more than
> 150Kb) if it is matter. Well.
> 
> Problem: m<-col2mat(w) works toooo slow. One row (that is 220 numbers)
> of such a matrix is being formed in ~5 sec! (5*2000 = 10000 sec). it
> is slowed in thousands times comparing to analogical C-code, too much
> I think.
> 
> Q1: is there a better way to create my matrix? (maybe I do some
>     mistake)
> 
> Q2(for developers): if this slowdown is a problem of R's engine, may
>                     we hope the problem will be corrected?
> 

A1: There is a better way. Vectorizing (and so eliminating the loops)
will speed up your function.

A2: R works as an interpreter. Loops in interpreted code are slow in
most cases.

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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