[R] More simple implementation is slow.

wl2776
Sat, 09 Jun 2012 09:08:22 -0000

Hi all.
I'm developing a function, which must return a square matrix.

Here is the code:
http://pastebin.com/THzEW9N7

These functions implement an analog of two embedded for cycles.

The first variant creates the resulting matrix by columns, cbind()-ing them
one by one.
The second variant creates the matrix with two columns, which rows contain
all possible
variants of i and j and calls apply on them.

The test input (data frame cp.table) can be produced with the following
commands:
> n<-132
> cpt<-data.frame(x=runif(n, min=0, max=100), y=runif(n, min=0, max=100),
> la=runif(n, min=0, max=360), phi=runif(n, min=-90, max=90))
Any random data will do.

The second variant seems to me much more readable and beauteful.
However, the first ugly variant runs much faster.
Why??
Here are the profiles:





--
View this message in context: