[Rd] Sending lists using .C

chris90nz chrisstevensphotography at ihug.co.nz
Sun Apr 22 02:49:14 CEST 2012


Hi,

I posted around a week ago and I will try this time with a more detailed
description.

I am converting R code to C code to boost speed as currently it takes a few
days to run. Now I will try describe this with as detail as possible.

I have a list of size 47. Each element of the list is itself a list of size
7, and each of these lists are arbitrarily long lists of integers.

I.e. If my main list is called Gcompat, then I could have Gcompat[[1]][[1]]
= 5, and Gcompat[[1]][[3]] = 1, 3, 6.

So Gcompat[[i]][[j]] = list of integers, where 1<=i<=47, 1<=j<=7. 

I have written the following code which will let me pass it through using .C
as two vectors, one containing the numbers contained in the lists all
concatenated, the other containing the indicies at which each list starts.

#########
Gcompat_values = vector()
Gcompat_lengths = vector()
Gcompat_indicies = as.vector(0)
   
for(i in 1:S) {
  for (j in 1:L) {
    Gcompat_values = c(Gcompat_values, Gcompat[[i]][[j]])
    Gcompat_lengths = c(Gcompat_lengths, length(Gcompat[[i]][[j]]))
  }
}
   
for (i in 2:(length(Gcompat_lengths)+1)) {
  Gcompat_indicies[i] = Gcompat_indicies[i-1]+Gcompat_lengths[i-1]
}
########

This approach works, but is still slow (10,000 times doing this takes
~30seconds).

I know you can pass a list through by having the C function take as a
parameter SEXP*. However I am not familiar with using Rinternals.h and am a
little lost as to how I would access elements of my big list if I just pass
the whole thing through to a SEXP*.

The other thing is I have 2 lists to pass through like this, and another
with an arbitrary sized matrix instead of a list of integers, so I think the
only way I can do this efficiently is if I do pass these lists through to a
SEXP*.

Any thoughts would be greatly appreciated.

Cheers,

Chris 

--
View this message in context: http://r.789695.n4.nabble.com/Sending-lists-using-C-tp4577449p4577449.html
Sent from the R devel mailing list archive at Nabble.com.



More information about the R-devel mailing list