Skip to content
Prev 307585 / 398506 Next

own function: computing time

Did not see a simple way to make it faster. However, this is a piece of 
code which can be made to run much faster in C. See below.

I don't know if you are familiar with running c-code from R. If not, the 
official documentation is in the R Extensions manual. However, this is 
not the most easy documentation for a first read. If you want to use the 
c-code and have problems getting it running, let me/us know your 
operating system and I/we will try to walk you through it.

HTH,
Jan


=== c-code ===
void foo(double* m, int* pn, int* r) {
   int n = *pn;
   double* pm1 = m;
   double* pm2 = m + n;
   int* pr = r;
   for (int i = 0; i < n; ++i, ++pm1, ++pm2, ++pr) {
     *pr = 1;
     double* qm1 = m;
     double* qm2 = m + n;
     for (int j = 0; j < n; ++j, ++qm1, ++qm2) {
       if ((*qm1 > *pm1) && (*qm2 > *pm2)) {
         *pr = 0;
         break;
       }
     }
   }
}

=== r-code ===
dyn.load("rtest.so")

foo <- function(m) {
   n <- dim(m)[1]
   .C("foo",
       as.double(m),
       as.integer(n),
       r = logical(n))$r
}

x <- runif(32000)
y <- runif(32000)
xy <- cbind(x,y)

t1 <- system.time({
     outer <- function(z){
         !any(x > z[1] & y > z[2])
     }
     j <- apply(xy,1, outer)
})

t2 <- system.time({
     j2 <- foo(xy)
})

=== results ===
 > all(j == j2)
[1] TRUE
 > t1
    user  system elapsed
  35.462   0.028  35.549
 > t2
    user  system elapsed
   0.008   0.000   0.008
 >
On 10/10/2012 12:15 PM, tonja.krueger at web.de wrote: