RfastOfficial/Rfast

dista performance

ggrothendieck opened this issue · 1 comments

It may be possible to speed up dista B is twice as fast as dista in the code below. See comments below this link

library(microbenchmark)
library(Rfast)
library(collapse)

m1 <- cbind(a=c(1,1,1,1,1,1,1,1), b=c(1,2,3,1,2,3,1,2), c=c(0,0,1,0,0,1,0,0))
m2 <- cbind(a=c(1,2,1,2,1,2,1,2), b=c(1,2,3,1,2,3,1,2), c=c(0,0,1,0,0,1,0,0))

microbenchmark(
  dista = dista.res <- dista(m1, m2, square = TRUE),
  B = B.res <- setop(rowsums(m1^2) - 2*tcrossprod(m1, m2), "+", rowsums(m2^2), 
		 rowwise = TRUE))
## Unit: microseconds
##   expr  min    lq   mean median    uq   max neval
##  dista 66.4 68.05 73.320   71.4 72.65 139.4   100
##      B 27.4 28.70 34.921   33.2 33.85 174.2   100

all.equal(dista.res, B.res)
# [1] TRUE

I have been testing this code and there are situations where it can lead to negative distances. I am unsure why that is.