Jason Liao <[EMAIL PROTECTED]> writes:

> I have a program which needs to compute squared Euclidean distance
> between two vectors million of times, which the Rprof shows is the
> bottleneck. I wondered if there is any faster way than my own simple
> function
>
> distance2 = function(x1, x2)
> {
>    temp = x1-x2
>    sum(temp*temp)
> }

You might try:

  distance2 <- function(x1, x2) crossprod(x1-x2)

And if you do not have to pass the distance2 function itself to some
other function, you might also spare the indirection trhough the
distance2 function and call crossprod directly (replacing the comma by
a minus sign :-).

-- 
François Pinard   http://pinard.progiciels-bpi.ca

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to