简体   繁体   English

优化R代码-Rcpp

[英]Optimization R code - Rcpp

In addition to benchmarking functions, is there any tool in R so we can fetch the biggest bottlenecks in an R code? 除了基准测试功能外,R中还有任何工具可让我们获取R代码中的最大瓶颈吗?

I often get very undecided about the computational gain I will get when rewriting the R code in C ++. 当我用C ++重写R代码时,我通常对获得的计算增益一无所知。 For example, in a bootstrap where each iteration needs to do an optimization, I do not know if it is useful to use the GSL library to do an optimization of a log-likelihood function, since the optim language function R uses the stats.so file. 例如,在每次迭代都需要进行优化的引导程序中,我不知道使用GSL库对对数似然函数进行优化是否有用,因为optim语言函数R使用stats.so文件。 I noticed this doing stats ::: C_optim . 我注意到这是在执行stats ::: C_optim

> stats:::C_optim
$name
[1] "optim"

$address
<pointer: 0x1cb34e0>
attr(,"class")
[1] "RegisteredNativeSymbol"

$dll
DLL name: stats
Filename: /usr/lib/R/library/stats/libs/stats.so
Dynamic lookup: FALSE

$numParameters
[1] 7

attr(,"class")
[1] "ExternalRoutine"  "NativeSymbolInfo"

Looking at the body of the optim function ( edit(optim) ), I see that there is the import of efficient functions implemented in C. For example, there is: 查看optim函数( edit(optim) )的正文,我发现导入了用C实现的高效函数。例如,有:

.External2(C_optim, par, fn1, gr1, method, con, lower, 
        upper)

Doubt : To Rcpp users, in your projects, do you normally try to implement all your C++ functions or implement a set of small C++ functions to be used in an R function? 疑问 :对于Rcpp用户,您通常在项目中尝试实现所有C ++函数或实现一组用于R函数的小型C ++函数吗?

I know it's a pretty general question, but all the functions I use Rcpp always try to implement C++ function from scratch. 我知道这是一个非常普遍的问题,但是我使用Rcpp的所有功能始终尝试从头开始实现C ++功能。 I felt that I'm programming more in C++ than in R. I sometimes think that I need to program directly in C++. 我觉得我用C ++编程比使用R语言更多。有时我认为我需要直接用C ++编程。

R has many characteristics that make the language slow for various tasks. R具有许多特性,使该语言在执行各种任务时变慢。 I always try to avoid loops and give way to the use of the apply family of functions. 我总是尽量避免循环,而让其使用apply系列函数。 However, I often find the R very slow. 但是,我经常发现R非常慢。 That way, because I'm very undecided on what's worth optimizing, I end up implementing everything in C++. 这样,由于我不确定什么值得优化,所以最终我用C ++实现了所有东西。

If you (generally) code faster in R and feel like writing to much C++ code, I suggest the following approach: 如果您(通常)使用R编写代码更快,并且想编写许多C ++代码,则建议采用以下方法:

  1. Implement your solution in R. 在R中实施您的解决方案。
  2. Only if the R solution is not fast enough, try to optimize it. 仅当 R解决方案不够快时,才尝试对其进行优化。
  3. The first step in optimization is measuring the performance, ie profile your code. 优化的第一步是测量性能,即分析代码。
  4. Once you have identified the bottlenecks you can improve those using, better R code or compiled code. 一旦确定了瓶颈,就可以使用更好的R代码或编译后的代码来改善瓶颈。

With experience you might be able to cut some corners, ie know from the beginning that some things in your problem will require compiled code. 凭着经验,您也许可以偷偷摸摸,例如,从一开始就知道问题中的某些事情将需要编译后的代码。 But that really depends on the kind of problems you are working on. 但这确实取决于您正在处理的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM