[Rd] speedup for as.matrix.dist

Romain Francois romain.francois at dbmail.com
Fri Apr 24 14:14:37 CEST 2009


Hi,

I found the init.c file I did not see before.

 > m <- expand.grid( x = 1:20, y = 1:20, z = 1:20 )
 > d <- dist( m )
 > system.time( out <- as.matrix( d ) )
   user  system elapsed
  3.006   1.252   4.429
 > old.as.matrix.dist <- function(x, ...)
+ {
+     size <- attr(x, "Size")
+     df <- matrix(0, size, size)
+     df[row(df) > col(df)] <- x
+     df <- df + t(df)
+     labels <- attr(x, "Labels")
+     dimnames(df) <-
+ if(is.null(labels)) list(1L:size,1L:size) else list(labels,labels)
+     df
+ }
 > system.time( out <- old.as.matrix.dist( d ) )
   user  system elapsed
 17.471   2.964  21.304


Romain

Romain Francois wrote:
> Hello,
>
> I am trying to patch as.matrix.dist to achieve some speedup.
>
> > m <- expand.grid( x = 1:20, y = 1:20, z = 1:20 )
> > d <- dist( m )
> > system.time( out <- stats:::as.matrix.dist( d ) )
>   user  system elapsed
> 15.355   3.110  19.123
> > system.time( out <- as.matrix.dist( d ) )
>   user  system elapsed
>  3.153   0.480   3.782


-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr


-------------- next part --------------
A non-text attachment was scrubbed...
Name: dist.diff
Type: text/x-patch
Size: 3124 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20090424/bbdddcc9/attachment.bin>


More information about the R-devel mailing list