[R] bad performance of a function

Petr Pikal petr.pikal at precheza.cz
Fri Nov 14 17:42:41 CET 2003


Hallo

Thanks to all who responded (Patrick Burns, Roger Bivand and 
especially Peter Dalgaard). Do not know why I used lapply when 
apply made the task as well. My actuall data are in data frame 
(size as in example 2) and with apply rle() is about an order of 
magnitude quicker.

Thanks again

Petr


On 14 Nov 2003 at 13:35, Petr Pikal wrote:

> Dear all
> 
> I need to find a length of true sequences in logical vector (see
> example 1). I found a possible solution which is good but if I use it
> on a larger data set I experience a substantial decrease in
> performance (example 2).
> 
> Example 1
> set.seed(111)
> x <- sample(c(T,F),50, replace=T)
> system.time(cetnost <-
> as.numeric(table(which(x)-cumsum(x[which(x)])))) [1] 0.00 0.00 0.03  
> NA   NA cetnost [1] 1 3 2 5 1 4 1 1 1 3 1 1 2
> 
> Example 2
> x<-sample(c(T,F),40321*51, replace=T)
> dd<-matrix(x,40321,51)
> system.time(cetnost <- lapply(dd,function(x)
> as.numeric(table(which(x)- cumsum(x[which(x)]))))) Timing stopped at:
> 750.63 1 775.6 NA NA 
> 
> Please give me any hint how to improve performance or advice a
> different (but more effective) solution.
> 
> R 1.8.0, W2000,  512M memory, Pentium4
> 
> Thank you in advance.
> 
> 
> 
> Petr Pikal
> petr.pikal at precheza.cz
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list