dmurdoch/rgl

[Optimization] Compute sin/cos outside for-loop

discoleo opened this issue · 3 comments

Optimization of turn3d

The following code appears in function turn3d:

theta <- seq(0, 2*pi, length.out = n + 1)[-(n + 1)]
for (i in inds) {
	vb <- cbind(vb, rbind(x[i], sin(theta)*y[i], cos(theta)*y[i], 1))

The sin(theta) and cos(theta) are computed during each iteration inside the for-loop. Furthermore, theta is a vector with 12 values: the repeated evaluation may become sufficiently expensive.

Show me the timings for this and your proposed improvement.

I was looking for a simple solution to rotate a protein (e.g. a ion channel monomer) around its central pore and generate the surface around the pore. The monomer may have hundreds of amino acids.

Using turn3d seemed a sufficiently simple hack. I do not have yet a functional code: but I have to think if it will work with 100 * (10 to 20 atoms) > 1000 atoms.

Don't worry about optimization yet. Keep your code simple, and get it to work. If it's fast enough, move on to the next task. If not, then worry about optimization.