将R代码转换为C代码 [英] Converting R code to C code

查看:162
本文介绍了将R代码转换为C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于速度原因以及将其打包为.exe的能力,我正在考虑将R脚本转换为C代码.我是C的新手.

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.

我的问题是,使用C语言会更快吗?速率限制步骤是一种排序算法,必须多次将其应用于大向量.我不确定R中的矢量化功能是否会帮助或降低它.我也读过for循环在R中效率低下.

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.

如果我应该在C中执行此操作,那么哪些库可以帮助我模仿R的某些数据处理功能(如基本矩阵操作)?我应该从哪里开始?现在,我什至不知道如何将数据读入C(逗号分隔的文本文件)中.

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).

推荐答案

我将尽力回答这个问题.

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

...但是您不问的问题可能更相关:R算法可以在R中更快地进行开发吗?答案通常是是".可以足够快"吗?好吧,如果不尝试(并查看当前的R代码),就不可能回答.

...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).

问:我的R算法在C语言中会更快吗?

Q: Will my R algorithm be faster in C?

A:是的!如果为该算法编写最佳" 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.

问:大型向量的排序可以在C语言中更快地完成吗?

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

A:是的.使用多线程,可以大大提高速度. ...但是从在R中调用sort(x, method='quick')开始,看看是否可以改善!对于随机数据,默认方法不是很快.

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)

问:哪些库模仿基本的R函数?

Q: What libraries mimic basic R functions?

A:LAPACK/BLAS处理R中的矩阵数学.如果仅此而已,您会发现比R中的原始库要快得多的库(您也可以在R中使用其中的一些库来提高性能!).

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!).

有关BLAS的更多信息

另一种方法是从R到C进行.Call,从那里您可以使用R的所有功能! inline软件包和Rcpp软件包可以帮助简化该过程.

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.

第三种方法是将R嵌入到您的应用程序中. Rinside可以使这一过程变得更容易.

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

问:如何将CSV数据读入C?

Q: How do I read CSV data into C?

A:查看fopenfscanf函数. ...并使用它们编写数据导入功能.

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

这篇关于将R代码转换为C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆