Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting R code to C code

Tags:

c

r

matrix

I'm looking into converting an R script into C-code for speed reasons and for the ability to have it packaged as an .exe. I'm new to C.

My question is will it be significantly faster in C? The rate limiting step is a sort algorithm that has to be applied a lot of times to big vectors. I'm not sure if the vectorized functionality in R will help this or slow it down. Also I've read that for-loops are inefficient in R.

If I should do this in C, what libraries might help me mimic some of the data-processing functions of R like basic matrix manipulations? Where should I look to get started? Right now I don't even know how to read my data into C (comma delimited text file).

like image 298
JoshDG Avatar asked Apr 13 '12 15:04

JoshDG


1 Answers

I'll try to answer this question as well as I can.

...but the question your NOT asking is perhaps more relevant: Can the R algorithm be made faster in R? The answer here is usually "yes". Can it be "fast enough"? Well, that is impossible to answer without trying (and seeing the current R code).

Q: Will my R algorithm be faster in C?

A: Yes! If you write the "best" C code for the algorithm, it will most likely be faster. It will most likely also be a lot more work to do so.

Q: Can sorting of large vectors be done faster in C?

A: Yes. Using multi-threading, you can improve the speed quite a lot. ...But start by calling sort(x, method='quick') in R and see if that improves things! The default method isn't very fast for random data.

x <- runif(1e7)
system.time( sort(x) )                   # 2.50 secs
system.time( sort(x, method='quick') )   # 1.37 secs
#system.time( tommysort(x) )             # 0.51 secs (4 threads)

Q: What libraries mimic basic R functions?

A: LAPACK/BLAS handles matrix math in R. If that's all you need, you can find libraries that are much faster than the vanilla ones in R (you can use some of them in R too to improve performance!).

More info on BLAS

Another way is to make a .Call from R to C and from there you have access to all of R's functionality! The inline package and the Rcpp package can help make it easier.

A third way is to embed R in your application. Rinside can help make that easier.

Q: How do I read CSV data into C?

A: Look at the fopen and fscanf functions. ...and use them to write a data import function.

like image 116
Tommy Avatar answered Sep 20 '22 16:09

Tommy