Re: [R] Speeding indexing and sub-sectioning of 3d array

From: Ray Brownrigg <Ray.Brownrigg_at_mcs.vuw.ac.nz>
Date: Mon 14 Aug 2006 - 14:27:02 EST


> Date: Thu, 10 Aug 2006 14:34:27 -0400
> From: "Swidan, Firas" <swidanf@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
>



R-help@stat.math.ethz.ch 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. Received on Mon Aug 14 14:43:44 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 14 Aug 2006 - 18:22:37 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.