[R] Speeding indexing and sub-sectioning of 3d array
Ray Brownrigg
Ray.Brownrigg at mcs.vuw.ac.nz
Mon Aug 14 06:27:02 CEST 2006
> Date: Thu, 10 Aug 2006 14:34:27 -0400
> From: "Swidan, Firas" <swidanf at janelia.hhmi.org>
>
> Hi Patrick,
>
> Thanks for the help. The function I listed is just an example. I isolated
> and kept only the problematic part in my code for clarity sake. I ended up
> implementing the functionality in C and now it takes 22 seconds to calculate
> the objective.
>
> Best regards,
> Firas.
>
Interestingly, I was able to develop an algorithm in R that achieves the
same order-of-magnitude speedup as your C code, but at the expense of
greater memory requirements. However it only works if the function you
are using is really is mean() [your code labels use Median]. It does
this by making use of cumsum() and logical indexing, working with sums
of values rather than calculationg the means and then dividing by the
numbers of values in the hypercube at the end.
If you want to try coding this algorithm in C for even greater
performance improvement (or for interest only), let me know. I suspect
it will be difficult to code in C because of the vectorisation it takes
advantage of.
In the output below, cK3d() is your algorithm (slightly adjusted to
cover the whole matrix and to return something), and cK3dme is my
equivalent, running on a Pentium IV 3.2GHz, NetBSD system with 1GB
memory.
Regards,
Ray Brownrigg
----
> x <- rnorm(245*175*150)
> dim(x) <- c(245, 175, 150)
> unix.time(yme <- cK3dme(x, 3))
[1] 13.870 1.690 15.813 0.000 0.000
> unix.time(y <- cK3d(x, 3))
[1] 500.206 0.035 505.738 0.000 0.000
> all.equal(y, yme)
[1] TRUE
>
----
More information about the R-help
mailing list